IOS: share documents between applications.

Source: Internet
Author: User

In iOS development, we need to share documents between applications in many cases. However, iOS provides a running sandbox for applications, and one application cannot communicate with another application directly, therefore, to share documents between applications, you must use the application interaction methods provided by some systems.


One simple means of interaction between iOS applications is "URL Scheme", which is to use the openURL method of UIApplication to specify an application in URL format such as "<Application name>, at the same time, you can add parameters similar to http get requests to the URL to transfer simple data. The code example is as follows:


[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "myapp: // test_page/one? Token = 12345 & user = ABC "];

 

The above code starts the application "myapp" and then passes the token and user parameters to the application "myapp.
However, such application calls cannot complete document sharing between applications, which can complete some simple data transmission. For more information about how to use URL Scheme, see LaunchMe in the official iOS example.


To share documents with other applications, the UIDocumentInteractionController class is generally used for initiating shared applications. by calling the presentOpenInMenuFromRect method of such instances, an "OpenIn" menu is displayed, this Menu displays the application icons declared in the system that support the documents you share. You can choose to use the application to open the shared application.


The following is a sample code. This example creates a UIDocumentInteractionController instance through the interactionControllerWithURL method of UIDocumentInteractionController, and then calls the instance's presentOpenInMenuFromRect method to display the "OpenIn" menu.


NSString * filePath =

[[NSBundle mainBundle]

PathForResource: @ "PDF Document" ofType: @ "pdf"];


NSLog (@ "file path is: % @", filePath );



Self.doc umentController =

[UIDocumentInteractionController

InteractionControllerWithURL: [NSURL fileURLWithPath: filePath];

Self.doc umentController. delegate = self;


Self.doc umentController. UTI = @ "com.adobe.pdf ";

[Self.doc umentController presentOpenInMenuFromRect: CGRectZero

InView: self. view

Animated: YES];

Place the above Code in the action method corresponding to a control. When this control is triggered, the system will pop up the "OpenIn" menu, which contains the program used by the user to open the application you share.
Note: All applications that claim to support the corresponding document type will appear in the "OpenIn" menu. For example, all application icons that "claim" to support the pdf format will appear in the "OpenIn" menu in the previous example, as to which application the user chooses, we cannot control whether the selected application can process the pdf document normally.

 


To use documents shared by other applications, the application receiving the documents must complete two tasks.
The first task is to add a declaration in Info. plist of the application to declare the document formats supported by the program. For example, if you need to support pdf documents, you can select the "Document types" section on the "Info" page on the configuration page of your application project, and add the configuration of the pdf Document type to it, for example:

 

 

 


The second task is to accept the data shared by other programs. The normal practice is to add the response code to the openURL method of the application and access the documents recorded in the parameter URL in the code, this URL points to a file in the inbox directory of the application's Documents, which is a copy of the file shared by other programs. The Code is as follows:

 


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

 

{

NSLog (@ "openURL method was called ");


NSLog (@ "the source application is: % @", sourceApplication );



NSLog (@ "url is: % @", url );


NSLog (@ "handle the file in above file path for the shared doc ");



Return YES;

}

 

The preceding openURL method prints the application name of the shared document and the path of the shared document in this application on the console, with this access path, developers can access the documents shared by other applications in this application.

 

 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.