-------------------------------------------------
If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for visiting!
Load Attachments In Your App!
A lot of developers want to be able to share their app data via email. It’s a convenient way for users to send data to each other or between devices – and it may even net you some new customers!
Luckily, this is pretty easy to do on the iPhone – you just have to set a few keys in your Info.plist and handle a few callbacks so the OS can open your app with the URL to import.
So we’ll cover how to do exactly that in this tutorial!
We’re going to start with the Scary Bugs project we’ve been building starting from the simple app tutorial on up to the file sharing tutorial.
So if you don’t have a copy of the project where we left off already, grab a copy!
Setting Up Your Info.plist
We’ve already laid most of the groundwork we need in order to support sharing our app data via email – we’ve written code to save a copy of our app data as a single file.
So the next thing we need to do is set up our Info.plist to let the OS know that we can handle “Scary Bug Documents”. The way you do this on iOS is by registering your app as able to handle certain UTIs, and exporting any UTIs that are not already known by the system.
In summary, UTIs are just unique identifiers that represent your document, such as “com.raywenderlich.scarybugs.sbz”. There are built-in ones for common document types such as “public.jpeg” or “public.html” as well.
So we’re register our app as being able to handle a UTI we make up for our app, and then we’re going to tell the OS a bit about our UTI, such as what file name extension it uses and what mime-type it’s encoded as in email.
So let’s see it in action! Open up ScaryBugs-Info.plist, and add the following entries:
You can read up on what each of these value’s mean in Apple’s UTI guide, but here are the important things to note: * The CFBundleDocumentTypes entry says what UTIs our app supports – in this case, the com.raywenderlich.scarybugs.sbz UTI, as an Owner/Editor. * The UTExportedTypeDeclaration entry gives some information about com.raywenderlich.scarybugs.sbz, since it isn’t a public UTI. Here we say that any file ending in .sbz or has a mime type of application/scarybugs is that kind of file.
Believe it or not, by setting these keys that’s all it takes for the OS to start sending our app files that end with .sbz. You can test it out by emailing yourself a copy of this sample bug if you want:
You can hold and tap on the attachment and it will prompt you if you want to open it in Scary Bugs. If you do, it will open up Scary Bugs, but of course it won’t load because we haven’t added the code for that yet! So let’s do that next.
Importing App Data
When Mail or some other app wants to send your app a file, it does so via one of two methods: via application:didFinishLaunchingWithOptions, passing the URL in the UIApplicationLaunchOptionsURLKey, or via application:handleOpenURL.
To understand what happens when, Oliver Drobnick has put together a very handy article with diagrams on what gets called when.
So let’s implement that real quick – it will be easy since we already have much of the supporting code in place. Make the following mods to ScaryBugDoc.h: // After @interface - (BOOL)importFromURL:(NSURL *)importURL; |
The following to ScaryBugDoc.m: // Add new function - (BOOL)importFromURL:(NSURL *)importURL { NSData *zippedData = [NSData dataWithContentsOfURL:importURL]; return [self importData:zippedData]; } |
The following in RootViewController.h: // After @interface - (void)handleOpenURL:(NSURL *)url; |
And the following in RootViewController.m: // New method