Transferring Documents From One Application To Another In iOS

If our app is capable of opening specific types of files, say pdf files by loading their content in a webView and if we want the pdf files in the device ( say as an attachment in email ) to be opened in our app, then we must register that support with the system.
This allows other apps,  to offer the user the option to hand off those files to our app.Whenever the user tries to open a pdf file he will be provided with an option to open that file in our app.

To declare the support for some specified file types, our app must include CFBundleDocumentTypes in info.plist

The CFBundleDocumentTypes key contains an array of dictionaries, each of which identifies information about a specific document type.

Each dictionary in the CFBundleDocumentTypes array can include the following keys:

1. specifies the name of the document type. (ex: PDF Document)CFBundleTypeName 2. is an array of filenames for the image resources to use as the document’s icon.CFBundleTypeIconFiles 3. LSItemContentTypes contains an array of strings with the UTI typesthat represent the supported file types in this group.

4.  ex: com.adobe.pdf

In order to start, some changes needs to be made in info.plist.

The new CFBundleDocumentTypes key needs to be added (along with the required keys).

When a PDF document is passed into the application, the application will fire a particular method – application:openURL:sourceApplication:annotation:. This method must be implemented in the application delegate.

When a document is passed into our application, it will be copied into a folder  located within the Documents folder. The url argument contains the path to the document.

-(BOOL)application:(UIApplication *)application

           openURL:(NSURL *)url

 sourceApplication:(NSString *)sourceApplication

        annotation:(id)annotation {    

  if (url != nil && [url isFileURL]) {

    [self.viewControllerhandleDocumentOpenURL:url];

  }    

  returnYES;

}

Once the document is passed in,  handleDocumentOpenURL: method will be called defined in some class to load the document in the WebView.

- (void)handleDocumentOpenURL:(NSURL *)url

{

  NSString* urlStr = [url absoluteString];

 

  UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"" message:urlStr

                                                 delegate: self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK",     nil];

[alert show];

  [alert release];

 

  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];        

  [_webViewsetUserInteractionEnabled:YES];    

  [_webView loadRequest:requestObj];

}

And now, if user tries to open a pdf say a pdf attached in a mail, he will see an option to open the pdf in our app.

like:  Open in….

When user taps open in…,  our app will be launched and the pdf will be opened in our app.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!