I need to have my app open documents from the Safari and Mail apps with that “Open In…” thing in the UIDocumentInteractionController class. How do I accomplish this?
I need to have my app open documents from the Safari and Mail apps
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I know this was extremely frustrating for me as a beginning programmer, or even as a moderately skilled one now. File I/O through the Mail and Safari apps involves very… interestingly named conventions within the app itself. So let’s get our hands dirty with an Xcode project for iPhone. Open Xcode (I will be using 4.2 for this Tutorial) and select the ‘Single View’ application template (or create an empty project, then add a single view with a .xib).
In that newly created application, rename the view controller (and associated xib) to
OfflineReaderViewController, and then we’ll get down to the code. (We will touch every file but the prefix header and main.m, so be aware that you’ll need everything in front of you!)Enter the AppDelegate header and paste the following code into it:
Then enter the Delegate’s .m file and paste the following code in verbatim:
This:
Is the singular most important part of this tutorial. To break it down into its respective parts:
-(BOOL)application:(UIApplication *)applicationis our sample app;openURL:(NSURL *)urlis the URL that’s sent to tell us what to open;sourceApplication:(NSString *)sourceApplicationis the application that sent the link; andannotation:(id)annotationis an extra feature we won’t get into.Now, we must layout our xib. Enter the xib (which should be entitled ‘OfflineReaderViewController’, but it doesn’t matter with a xib, unless we call
initWithNibName:(which we won’t)), and make it look like the picture below:It is VERY important that you go into the
UIWebView‘s Attributes and check “Scales Pages To Fit”, as this let’s us zoom in and out on web pages with pinches. Don’t worry about the connections just yet, we will be creating those shortly.Enter the
OfflineReaderViewControllerheader and paste in the following:Now the .m:
Those of you who are actively watching and not just copying everything I tell you to (just kidding) will know that this line:
[[NSBundle mainBundle] pathForResource:@"Minore" ofType:@"pdf"];will give us a SIGABRT because, well, the file doesn’t exist! So, drag in any generic PDF that you’ve pulled from wherever (I recommend here because who doesnt spend their free time reading massive amounts of documentation?), then copy its title and paste it in with the suffix (.pdf) removed; theofType:@"pdf"part takes care of that for us. The line should look like this when you’re done with it:[[NSBundle mainBundle] pathForResource:@"//file name//" ofType:@"pdf"];Now go back into the xib and hook up those
IBOutlets! All told, here’s what your “File’s owner” tab should look like:It seems we’re done…but wait! We didn’t do anything to get an “Open In…” menu up and running! Well, it turns out that there is some mucking around in the .plist file necessary. Open up the app .plist (with a quick right click, then select Open As > Source Code) and paste in the following:
[Side note: be careful mucking around in the source code of any plist, if you don’t know what you’re doing, you could get the dreaded ‘This file has been corrupted’ error from Xcode]
If one were to right click and select Open As > Property List, it would look like this:
There’s another VERY important field in there called ‘Application supports iTunes file sharing’. That must be set to “YES”, or your app will not show up in iTunes as supporting file sharing.
The ‘Document Types’ field specifies the kinds of documents our example can open. Expand the arrow to find its role and UTI’s. These are unique identifiers (Unique Type Identifiers; seems obvious what that acronym means now, doesn’t it?) that every kind of file has. UTI’s are what let the finder replace a generic document image with that nice localized image of the file type (don’t believe me, rename an unimportant file extension to .ouhbasdvluhb and try to get a nice picture!) If I wanted to open my own custom format (lets say a .code file) then I would put something like
com.CodaFi.code(reverse DNS notation for those with no clue) in the UTI field and Document Type Name would be ‘CodaFi Document’. Handler Rank and Role should be straightforward as our handler rank is alternate (because we don’t own the file) and our role is viewer (because we don’t need anything more important. Our example is just a viewer and not an editor, so we’ll leave it as such.For future reference, UTI’s have official system-declared naming schemes when they come from respected sources (Oracle, Microsoft, even Apple itself) which can be found in the Uniform Type Identifier Reference Guide, but are listed here for pedantry’s sake.
Now, let’s run ‘er! The code should build with no errors, assuming you copied verbatim and got those darned xib hookups right. Now when you first launch your application, you should be presented with the option to open a document in iBooks. Deselect it, the real meat of the code is opening other documents! Launch Safari and search for any PDF that Safari can QuickLook or open. Then in the “Open in…” menu, our app shows up! Click it. You’ll get the little switcheroo animation and an alert will come up with the location of the file. When you dismiss it, the
UIWebViewwill have loaded the PDF. The Mail app has similar functionality with attachments. You can also call those PDFs up to your app.That’s it, it’s all done. Enjoy and happy coding!