[self blog]

iOS and Objective-C development

Libraries for Handling Compression

Do you need to handle compressed files on your iOS project? Then you may find the following links to compression libraries useful:

  • ZipKit is an Objective-C framework for reading and writing Zip archives in Mac OS X and iOS apps.
  • Unrar4iOS The main goal of this project is provide a port of Unrar library to iOS platform.
  • 7-Zip LZMA SDK LZMA is the default and general compression method of 7z format. The LZMA SDK provides the documentation, samples, header files, libraries, and tools you need to develop applications that use LZMA compression.

XPath Query Returns No Results Using Libxml2 and GDataXmlDocument nodesForXPath

If you are using TouchXML or GDataXML for reading XML documents, you may have run into a problem with your XPath queries. I was working with the Freshbooks API to return a list of projects and it returned some fairly simple XML (I have abbreviated all the attributes of a project element for clarity):

Using the Freshbooks API to return a list of projects Freshbooks API Source
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<response xmlns="http://www.freshbooks.com/api/" status="ok">
  <projects page="1" per_page="15" pages="1" total="5">
    <project>
      <name>Super Fun Project</name>
    </project>
  </projects>
</response>

So now I wanted to simply retrieve all the project elements in the response using XPath. Seemed simple enough, so this is what I wrote:

Retrieve objects using XPath and GDataXMLDocument
1
2
3
// doc is an instance of GDataXMLDocument
NSArray *projects = [doc nodesForXPath:@"//projects/project" error:nil];
NSLog(@"%d", projects.count);

My NSLog statement always returned 0 results. This started to drive me nuts. I tried several different XPath queries and still no luck. There were no errors even when I used the error outlet, and I could even loop through the elements manually and see that there were indeed project elements being returned. So what’s the problem?

The problem had to do with namespaces. The XPath query needed to be written using the correct XML namespace, so something like this:

Adding a namespace to GDataXMLDocument
1
2
3
// doc is an instance of GDataXMLDocument
NSArray *projects = [doc nodesForXPath:@"//ns:projects/ns:project" error:nil];
NSLog(@"%d", projects.count);

Great, but what is the namespace name? According to my XML response it was http://www.freshbooks.com/api/. However the GDataXMLDocument knew nothing about it.

I started digging into the source of GDataXML and came across this in the nodesForXPath method:

GDataXML nodeForXPath Source Excerpt
1
2
3
4
5
6
int result = xmlXPathRegisterNs(xpathCtx, prefix, nsPtr->href);
if (result != 0) {
#if DEBUG
    NSCAssert1(result == 0, @"GDataXMLNode XPath namespace %@ issue",prefix);
#endif
}

Ok… some simple debugging showed it was actually registering a namespace, but what was it called? That lead me to some code just before the xmlRegisterNs call:

GDataXML nodeForXPath Source Excerpt
1
2
3
if (prefix == NULL) {
    prefix = (xmlChar*) kGDataXMLXPathDefaultNamespacePrefix;
}

Now we’re getting somewhere. Next stop the definition of the kGDataXMLXPathDefaultNamespacePrefix constant:

kGDataXMLXPathDefaultNamespacePrefix Definition
1
2
3
// when no namespace dictionary is supplied for XPath, the default namespace
// for the evaluated tree is registered with the prefix _def_ns
_EXTERN const char* kGDataXMLXPathDefaultNamespacePrefix _INITIALIZE_AS("_def_ns");

THAT WOULD HAVE BEEN HELPFUL AN HOUR AGO… Anyway, I went ahead and changed my original XPath query to:

Adding the correct default namespace to GDataXMLDocument
1
2
3
// doc is an instance of GDataXMLDocument
NSArray *projects = [doc nodesForXPath:@"//_def_ns:projects/_def_ns:project" error:nil];
NSLog(@"%d", projects.count);

BOOM. Now we’re cooking with gas. It correctly returned all my project elements. So the lesson learned here is that GDataXML and TouchXML will not register your namespace or use it by default. You have to use the default, or register it manually.

How do you register a namespace with GDataXML manually you ask?

Registering a namespace with GDataXML
1
2
3
4
// doc is an instance of GDataXMLDocument
NSDictionary *ns = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.freshbooks.com/api/", @"fb", nil];
NSArray *projects = [doc nodesForXPath:@"//fb:projects/fb:project" namespaces:ns error:nil];
NSLog(@"%d", projects.count);

And finally, if you want to programatically register a namespace using GDataXml, then you would populate your NSDictionary by iterating through the namespaces found by GDataXMLElement:

Programmatically register a namespace with GDataXML
1
2
// doc is an instance of GDataXMLDocument
NSArray *namespaceURIs = [doc.rootElement namespaces];

Hopefully that saves you some time and sanity.

Core Animation Techniques

Just came across this great demo of using Core Animation in your iOS application. Its basically a demo application that allows you to try out each technique and then you can review the source. The project is on Github. You should also check out Bob McClune’s Blog, the creator of the demo.

Pro Tip: Working With SQLite Databases for iOS Development

When I do web development I always have some database manager open so that I can quickly review the data used by my application. So naturally with Core Data development, I want to be able to do the same thing. Since Core Data is working with a SQLite database, all you need is a SQLite database manager on your Mac. I have found two that I like:

Base This one costs $30 on the App Store.

SQLite Manager for Firefox This one will cost you nothing and is an addon for Firefox.

I also like to set a shortcut to the following path on my Finder:

/Users/YourUsername/Library/Application Support/iPhone Simulator/

This will help you quickly drill into the various application directories. You’ll find the SQLite database inside the Documents folder of your application inside the iPhone Simulator directory.

How to Enable SQL Debugging in Xcode 4

If you would like to output the SQL being generated by Core Data during iOS development you need to add an entry to the Arguments Passed On Launch for your app. Its as easy as:

  1. Go to Product -> Edit Scheme
  2. Choose Run YourAppName.app on the left panel
  3. Add -com.apple.CoreData.SQLDebug 1 to the Arguments Passed On Launch panel

I found this answer on StackOverflow.