iPad SDK 3.2 Document Support

In this post, we’ll discuss about the new File-Sharing and Document Support features in iPhone OS 3.2. The new File-Sharing feature allows user to access /Document directory through iTunes. Users are able to move files from their computers to iPads or iPads to their computers by using this feature. The new Document Support feature allows an application to register its supportive file types to the system. This feature allows an application to redirect files to be opened in other applications. For an example: an application doesn’t know how to open a PDF document. This feature allows the PDF document to be opened in other applications which can open PDF format.

In the section 1, we discuss XCode configuration in order to enable the document sharing feature between computers and iPad via iTunes. In the section 2 and 3, we’ll be creating 2 applications to discuss about the new Document Support features between iPad Apps. Our Applications will be called “DocumentOpener” and “DocumentReader”. DocumentOpener will be simply redirect files to be opened in appropriate applications. DocumentReader will accept files that DocumentOpener redirected and display them.

1. File Sharing with iTunes

First, create a OS 3.2 view based project in XCode. We’ll add the File-Sharing support to this application first. Adding File-Sharing support is as simple as adding a new key in info.plist file.

Open your info.plist file and add the following key under Information Property List: “UIFileSharingEnabled”.

Right Click on the Value Type column and set it as boolean type.

Install the application on iPad and connect it to iTunes.

Go to the application in iTunes, and you should now be able to see /Document directory. You can now move around your files between your application and your computer through iTunes. Sometimes you may not see the /Document directory when you connect your iPad to iTunes. When this happens, you just simply need to disconnect and reconnect your iPad to iTunes again.

2. Opening a document in other applications

Next open up your xib file and add a toolbar and Bar Action Bar Button to your view.

DocOpenerXib

In your Viewcontroller.h file add the following code:

@interface ipadDocOpenerViewController : UIViewController {
IBOutlet UIBarButtonItem *mItem;
UIDocumentInteractionController *controller;

}

You need to retain the UIDocumentInteractionController or it won’t work properly.

UIDocumentInteractionController can have 4 types of menu:

  1. Quick View
  2. Open In “the most recent or most view application”
  3. Open In
  4. Action

2.1 Open In …

OpenIn menu will allow user to select from a list of application that can support the target document. In our viewController.h file we decleared a method:

-(IBAction) clickOpen:(id)sender)

Let’s implement the method in our viewcontroller.m

-(IBAction) clickOpen:(id)sender {
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"License"  ofType:@"pdf"];
self.controller = [UIDocumentInteractionController  interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]];
self.controller.delegate = self;

CGRect navRect = self.navigationController.navigationBar.frame;
navRect.size = CGSizeMake(1500.0f, 40.0f);
[self.controller presentOptionsMenuFromRect:navRect inView:self.view  animated:YES];

}

First, we create the NSString that stored the URL.Next we initialize our UIDocumentINteractionConroller with that URL.  In the next step, we create a rectangle to display our menu, and we present our DocumentController menu inside our main view. Do not forget to make appropriate connection in interface builder to our clickOpen method and UIBarButtonItem.

Here is our DocumentOpener in action:

DocOpenerFinish

2.2 Quick View

iPhone OS 3.2 support in document open preview option for supported document type. What this mean is that even if your application doesn’t support the file type of a document, you can still open the document inside your application if the file type is  supported by iPhone OS (Example pdf, jpeg).  Yes, you can open a pdf file in any application by using quick view feature. Let’s add the quick view to our DocumentOpener menu.

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}

QuickView In Action:

QuickVIew This conclude the first part of our tutorial. In our next section, we’ll create an application that act as document server. We’ll discuss about how to open the documents coming from the DocumentOpener application.

3. Accepting Incoming Documents (Document Reader)

Let’s create a iPhone OS 3.2 view based application called “DocumentReader”. To accept incoming documents, first we need to declare which document types that our application can support. Second we need to get the URL of the incoming file.

3.1 Declearing support document types

Open up your info.plist file. For our application, we’ll add PDF support, so our other applications can see our DocumentReader in their OpenIn menu.

infoPlistDocTypes

UTI (Uniform Type Identifiers) make it easier to declare document types in iPhone OS environment. For more bout UTI, please visit the following link:

http://developer.apple.com/macosx/uniformtypeidentifiers.html

You can add multiple document support types in your info.plist file. As long as they are properly declare, other apps can easily pick up your application by this key automatically.

3.2 Reading the URL of an incoming document

To read the URL of a document coming from other application, all you need to do is implement “applicationDidFinishLaunching” and “application didFinishLaunchingWithOptions” methods.

- (void)applicationDidFinishLaunching:(UIApplication *)application
{

[window addSubview:viewController.view];
[window makeKeyAndVisible];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self applicationDidFinishLaunching:application];

if (launchOptions && [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey])
{
printf("Launch options:\n");
CFShow(launchOptions);

NSString *filePath = [NSString stringWithFormat:@"%@", (NSURL*)[launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]];
viewController.mLabel.text = filePath;
[viewController.mWebView loadRequest:[NSURLRequest requestWithURL:(NSURL*)[launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]]];

}

return YES;
}

In our code, we simply read incoming URL and display the file in a simple UIWebView. This conclude our tutorial on File-Sharing and Document Support. Happy Coding.

Download Source Code (XCode Projects): DocOpenerReader

By: kiichi on: