IOS Document Interaction Programming Guide

Source: Internet
Author: User
Tags file url

About DocumentInteraction


 
IOS supports previewing and displaying documents with other apps in your app. IOS also supports file association, allowing other apps to open files through your program. These technologies include the UIDocumentInteractionController Class Reference provided by UIKit and the Quick Look Framework Reference ).

 

Since iOS4.2, the Quick Look framework provides built-in file printing support. If you want to customize your own printing and copying behaviors, You can implement the documentinteraction controller delegate method to provide support. For sample code of the UIDocumentInteractionController and QLPreviewController classes, see DocInteraction sample project.

Preview document and present option menu

If your app needs to open files that it does not support, use UIDocumentInteractionController. A documentinteraction controller uses the Quick Look framework to determine whether a document can be opened and previewed by another app. That is to say, your app provides users with optional operations through documentinteraction controller.

To use a document interaction controller, follow these steps:

1. Create a UIDocumentInteractionController instance for each file you want to open.

2. Provide an image representation of the file in the app UI (generally, you should display the file name or icon ).

3. If the user interacts with this object, for example, by touching this control, the document interaction controller is called to display the following interface:

O preview the content of the file.

O A menu containing preview and open operations. You can add other operations to the menu by implementing some delegate methods, such as copying and printing.

O a menu that only contains the "open in other ways" operation.

Documentinteraction controller has built-in gestures that you can directly implement their actions.

Document interaction controller can be used for apps that need to interact with files. Most of these programs need to download files from the network. For example, the email program needs to open and preview the email attachment.

Document interaction controller is required even for apps that never download files. For example, your app must Support File Sharing (refer to "File-Sharing Support" in iOS Technology Overview and DocInteraction example project ), you can use documentinteraction controller for files synchronized to the app Documents/Shared directory.

Create DocumentInteraction Controller
Create a document interaction controller instance, instantiate it with the file you want to open, and set its delegate attribute. The delegate object informs the document interaction controller of the information required to present the view and the actions to be executed when the view is displayed and interacted with the user.

See the following code. Note that the caller of the method must retain the returned object.

 

-(UIDocumentInteractionController *) setupControllerWithURL: (NSURL) fileURL
UsingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
 
UIDocumentInteractionController * interactionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
InteractionController. delegate = interactionDelegate;
 
Return interactionController;
}

 

After creating the document interaction controller, you can use its attributes to read the information of the associated file, including the file name, type, and URL. The controllery also has an icons attribute, which contains multiple UIImage objects. This attribute is used to indicate multiple icons of the document size. This information can be used in the UI.

If you want users to open this file in other programs, you can use the annotation attribute of controller to put the additional information required by the program. Of course, the information format must be recognized by the application. For example, this property will be used by a single program in an application suite. The annotation attribute can be used when the program wants to interact with other programs in the suite. When an application is called to open a file, the option dictionary contains the annotation value. You can use UIApplicationLaunchOptionsAnnotationKey as the key to retrieve it in the option dictionary.

Render Document InteractionController
With Documentinteraction controller, You can preview the file or use the pop-up menu to select the corresponding action.

· In the mode display file preview window, call presentPreviewAnimated: method.

· The pop-up menu prompts you to select the action and call presentOptionsMenuFromRect: inView: animated: Or presentOptionsMenuFromBarButtonItem: animated: method.

· Prompt the user to open the file with another program and call presentOpenInMenuFromRect: inView: animated: method or presentOpenInMenuFromBarButtonItem: animated: method.

These methods will display a view-either a preview window or a pop-up menu. The return value must be checked for any method call. The returned value is NO, indicating that the view does not have any content and cannot be displayed. For example, the presentOpenInMenuFromRect: inView: animated: method returns NO, indicating that NO program in the installed program can open this document.

To display the preview window, the delegate object must implement documentInteractionControllerViewControllerForPreview: method. The preview window is displayed as a mode window. Therefore, you need to return a view controller in this method as the parent window of the preview window. If you do not implement this method, or return nil in this method, or the view controller you return cannot render the mode window, the preview window will not be displayed.

Document interactioncontroller will automatically disband the window it presents. Of course, you can also call dismissMenuAnimated: Or dismissPreviewAnimated: to manually disband it.

In the example DocInteraction project, it demonstrates how to present a document interaction controller (using Gesture Recognition ).

 

Types of documents supported by registered applications

If your program can open a specific file type, you can use the Info. pllist file registration program to open the file type. When other programs ask the system which programs can recognize this type of files, your programs will be listed in the Options menu for users to choose from.

Related chapter: "register the file types supported by the application"

File Types supported by Application Registration
If your program can open a file, you can register it with the system. This will run other programs using the document interaction Technology of iOS to provide users with a choice, and thus call your program to process these files.

This requires that the CFBundleDocumentTypes key be included in the Info. plist file of the Program (see "CoreFoundation Keys "). The system registers the content contained in this key so that other programs can access this information through document interaction controller.

The CFBundleDocumentTypes key is a dictionary array, and a dictionary represents a specified document type. A document type usually corresponds to a certain file type. However, if your program uses the same processing method for multiple file types, you can also divide these types into one group and treat them as one document type. For example, one of the local document types used in your program is in the old format, and the other is in the new format (It seems to be an image of Microsoft Office documents ), you can divide the two into one group and put them in the same document type. In this way, files in the old and new formats are displayed as the same document type and opened in the same way.

Each dictionary in the CFBundleDocumentTypes array may contain the following keys:

· CFBundleTypeName: Specifies the document type name.

· CFBundleTypeIconFiles is an array containing multiple image file names used as icons for this document.

· LSItemContentTypes is an array containing multiple UTI strings. The UTI type is the file type included in this document type (group.

· LSHandlerRank indicates whether an application is "owned" or simply "open.

From an application perspective, a document type is actually a file type (or multiple file types), which treats all files of the same document type as the same. For example, an image processing program may regard various image files as different document types, which facilitates Optimization Based on each type. However, for a word processing program, it does not care about the real image format. It treats all the image formats as a document type.

The following table lists a CFBundleTypeName example in Info. plist.

LSItemContentTypes indicates the UTI corresponding to the file format. CFBundleTypeIconFiles specifies the ICON resources used for this type.

Table 1 document types of custom file formats

<Dict>
<Key> CFBundleTypeName </key>
<String> My File Format </string>
<Key> CFBundleTypeIconFiles </key>
<Array>
<String> MySmallIcon.png </string>
<String> MyLargeIcon.png </string>
</Array>
<Key> LSItemContentTypes </key>
<Array>
<String> com. example. myformat </string>
</Array>
<Key> LSHandlerRank </key>
<String> Owner </string>
</Dict>

For more information about the CFBundleDocumentTypes key, refer to the InfoPlist key reference.

 

Open files in other applications

The system may request a program to open a file and display the open option menu to the user. The most common case is that an application has registered a supported file type. In this way, the system will pass the File URL to your application and put it in the foreground.

Related chapter: "Open supported file types"

Open supported file types
The system may request a program to open a certain file and present it to the user. This usually occurs when an application calls document interaction controller to process a file. You can delegate

Application: didfinishlaunchingwitexceptions: Obtain the file information in the method. If your program needs to process some custom file types, you must implement this delegate method (instead of applicationDidFinishLaunching: method) and use this method to start the application.

 

Application: didfinishlaunchingwitexceptions: The option parameter of the method contains information about the file to be opened. In particular, you need to care about the following keys in the program:

· UIApplicationLaunchOptionsURLKey contains the NSURL of the file.

· UIApplicationLaunchOptionsSourceApplicationKey contains the Bundle ID of the application sending the request.

· UIApplicationLaunchOptionsSourceApplicationKey contains the attribute list objects related to the file passed by the source program to the target program.

If the UIApplicationLaunchOptionsURLKey exists, your program should immediately open the file with this URL and present the content to the user. Other keys can be used to collect parameters and information related to opened files.

 

Preview and print in Quick Look Previews

To get more file preview functions, you can directly use the Quick Look framework. You can select the animation style when the preview window is displayed, and you can preview multiple files as you want to preview a single file. QuickLook preview controller also has built-in support for AirPrint printing of file types.

Related chapter: "Using Quick Look framework"

Use Quick Look framework
 

The Quick Look framework provides enhanced preview functions. This framework mainly provides the QLPreviewController class. This class depends on the delegate object to respond to the preview action and a data source used to provide the preview file.

Starting from iOS 4.2, QuickLook preview controller provides a preview view containing an action button (that is, a print button. For files that can be previewed by the controller, the action button can print this document. You do not need to write any printed code.

You can use the following method to display a Quick Look preview controller:

· Use the navigation controller to display the preview window in push mode.

· Display in a modal window using the presentModalViewController: animated: Method of UIViewController.

· Display a document interaction controller (as described in "preview and open a file ). You can select "Quick Look" from the Options menu of document interaction controller to open a QuickLook preview controller.

When displaying Quick Lookpreview controller, select the appropriate appearance and navigation method for your application. If your program does not use a navigation bar, it is appropriate to use a full screen mode Quick Lookpreview controller. If your program uses an iPhone-style navigation, it is appropriate to display the preview window in push mode.

The preview window contains a title that displays the last path of the File URL. To overload the title, you can customize the PreviewItem class and implement the previewItemTitle method in the QLPreviewItem protocol.

Quick Look previewcontroller can preview the following files:

· IWork document

· Microsoft Office documentation (for Versions later than Office '97)

· Rich Text Format (RTF) Document

· PDF document

· Images

· Text file, whose uniform type identifier (UTI) is defined in the public. text File (view the UniformType Identifiers reference)

· Comma-separated value (csv) File

To use QuickLook preview controller, you must specify the data source object (that is, implement the QLPreviewControllerDataSource Protocol). For more information, see QLPreviewControllerDataSource protocol reference ). The data source provides preivew items for Quick Look preview controller and specifies their quantity to include them in a preview navigation list. The list contains multiple objects and a navigation arrow is displayed in the modal preview window (full screen) to switch between multiple preview objects. For QuickLook preview controller displayed in the push mode of the navigation controller, You can provide buttons on the navigation bar to see the switch in the preview Object List.

 

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.