iOS Tips and Tricks: Associate a File Type with your App - Part 3

Stephen Zaharuk / Friday, March 15, 2013

Part 1: Standard File Extensions

Part 2: Custom File Extensions

In this final part of the series we'll finally learn how to react to those files being added to your project. 

Step 1. Open your AppDelegate.m file. 

Step 2. Add the following method:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

Step 3. Now, when you import a file, that method will get called.  The file is actually stored in a folder called "inbox" which can be found in the documents directory. Here is the code to get all files in that directory:

NSFileManager *filemgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* inboxPath = [documentsDirectory stringByAppendingPathComponent:@"Inbox"];
NSArray *dirFiles = [filemgr contentsOfDirectoryAtPath:inboxPath error:nil]; 

And there you go, you can now loop through those files and consume them however you need to. 

As always, I hope this was helpful!

By Stephen Zaharuk (SteveZ)