Ios sdk: preview and open a document

Source: Internet
Author: User
Tags uikit

Http://mobile.tutsplus.com/tutorials/iphone/previewing-and-opening-documents-with-uidocumentinteractioncontroller/

Sandbox in IOS makes the platform more secure, which is also the main benefit of sandbox for users. However, due to the strict restrictions on sandbox, It is troublesome to share data between programs. Generally, documents can be shared between programs through uidocumentinteractioncontroller (this type is often ignored by developers ). This article describes how to use this class to preview and open documents in other programs (programs already installed on the device.
 
Uidocumentinteractioncontroller already exists in IOS 3.2, which is flexible and powerful. In addition to sharing documents between apps on the same device, the tool also allows you to preview, print, send emails, and copy documents.
 
The use of uidocumentinteractioncontroller is very simple. First, call its unique class method interactioncontrollerwithurl: and input a URL (nsurl) to initialize an instance object. Set the delegate attribute of the View Controller and implement some proper delegate methods.
 
Note that uidocumentinteractioncontroller is not a subclass of uiviewcontroller. Therefore, you must notify document interaction controller of the View Controller Used to preview the document.
 
Step 1: set the project
It is very useful to open a document between apps on the same device. A common example is to open an image in the image editor, such as iPhoto.
 
Create a new project in xcode and select the "single view application" template. Name the document, enter the company identifier, select iPhone for device, and select "use automatic reference counting" for device. Click "Next", save the project, and click "creat.


 Step 2: create a user interface
The User Interface of this program includes two buttons. One is used to preview the PDF document in other apps, and the other is used to open the PDF document in other apps. Before creating a user interface, assign an action to each button in the viewcontroller execution file, as shown below:
1.-(ibaction) previewdocument :( ID) sender {
2 .}
 
1.-(ibaction) OpenDocument :( ID) sender {
2 .}
 
Select mtviewcontroller. XIB. We need to drag two uibutton instances from the View Controller view on the right. Select File's owner objectobject on the left, open connections inspector, and connect the previously created action with the button.
 

Step 3: preview the document
Currently, PDF documentation is supported. You can use any PDF document. However, in the source file about this technique, I have included a PDF example, which is Apple's iOS programming guide, it can also be obtained online. Drag the document to your project, select the "Copy items into destination group's folder (ifneeded)" check box, and finally ensure that the file has been added to the "Documents target" below.

Note: 1. You need to save the document interation controller instance. 2. Implement the uidocumentinteractioncontrollerdelegate protocol.
 
First, update the header file of viewcontroller (as shown below) to inform compiler that the mtviewcontroller class complies with the uidocumentinteractioncontrollerdelegate protocol.
1. # import <uikit/uikit. h>
2. @ interface mtviewcontroller: uiviewcontroller <uidocumentinteractioncontrollerdelegate>
3. @ end
 
In the View Controller implementation file, add a private attribute of the type uidocumentinteractioncontroller and name it documentinteractioncontroller. This attribute stores document interaction controller and will be used later.
 
Take a look at the previewdocument: method implementation. First, obtain the document URL (nsurl). Because the document is part of the app, it is very easy to obtain the document (nsurl) through the nsbundle class, as shown below:
 
1.-(ibaction) previewdocument :( ID) sender {
2. nsurl * url = [[nsbundle mainbundle] urlforresource: @ "sample" withextension: @ "pdf"];
3. If (URL ){
4. // initialize document interactioncontroller
5. self.doc umentinteractioncontroller = [uidocumentinteractioncontroller
Interactioncontrollerwithurl: url];
6. // configure document interactioncontroller
7. [self.doc umentinteractioncontrollersetdelegate: Self];
8. // preview PDF
9. [self.doc umentinteractioncontroller presentpreviewanimated: Yes];
10 .}
11 .}
 
If a valid URL is returned, we initialize a uidocumentinteractioncontroller instance and use the URL of the document. In the documentinteractioncontroller attribute, we store a reference to document interaction controller. View Controller will act as the delegate of document interaction Controller
 
If you run the app now, you will notice that nothing happens when you click the preview button. Because here we need to implement a delegate method first.
 
As mentioned above, the uidocumentinteractioncontroller class is not a subclass of uiviewcontroller, but inherited from nsobject. We need to notify documentinteraction controller which view controller is used for document preview.
 
Uidocumentinteractioncontrollerdelegate has a delegate method named documentinteractioncontrollerviewcontrollerforpreview:. This method requests a viewcontroller used to display (PreView) the document.
 
We want to display the preview in main viewcontroller, so we can simply return self, as shown in the following code. It means that document interfation controller will use our view controller to preview the PDF document-display the document in modal view mode.
 
1.-(uiviewcontroller *) documentinteractioncontrollerviewcontrollerforpreview:
(Uidocumentinteractioncontroller *) controller {
2. Return self;
3.
 
Of course, you can simplify the implementation of documentinteractioncontrollerviewcontrollerforpreview: to meet your needs. When executing the delegate method, you can run the app for the first time and try it out. You can share this document via email and print or copy it. In addition, you can open the document in other apps that support this document type, click the button on the right of the graph to see what I mean.

Step 4: Open the document
To achieve this goal, we need to implement OpenDocument: method. In the OpenDocument: method, obtain the URL of a PDF file in the bundle, and use this URL to initialize a uidocumentinteractioncontroller.

 

Set the delegate of the uidocumentinteractioncontroller and call presentopeninmenufromrect: inview: Method to display a menu in the uidocumentinteractioncontroller. The first input parameter cgrect is the frame of the button, as shown below:
1.-(ibaction) OpenDocument :( ID) sender {
2. uibutton * button = (uibutton *) sender;
3. nsurl * url = [[nsbundle mainbundle] urlforresource: @ "sample" withextension: @ "pdf"];
4. If (URL ){
5. // initialize document interactioncontroller
6. self.doc umentinteractioncontroller = [uidocumentinteractioncontroller
Interactioncontrollerwithurl: url];
7. // configure document interactioncontroller
8. [self.doc umentinteractioncontroller setdelegate: Self];
9. // present open in menu
10. [self.doc umentinteractioncontrollerpresentopeninmenufromrect: [Button frame] inview: Self. View animated: Yes];
11 .}
12 .}
 
To test OpenDocument: method, it is very important to run the instance app on a real machine. The reason is very simple. The operating system should check whether the installed app supports the file type (UTI) We want to open ). If no app supports the corresponding file type is found, no prompt will be displayed in the menu, and this cannot be tested using the IOS simulator.
 
To test this function, you must first install an app that supports pdf on a real machine, such as Dropbox or Amazon's Kindle app.

Summary
You can use the uidocumentinteractioncontroller class to preview and open documents between apps. We recommend that you read the reference documents of this class, especially the uidocumentinteractioncontrollerdelegate protocol-there are many delegate


Methods: these methods are very convenient when you encounter large documents or complex workflows.

Source file:

 

Http://download.csdn.net/detail/ydj213/5405941


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.