Use of iOS development--uidocumentinteractioncontroller (for more sharing services)

Source: Internet
Author: User

Recently in the project, encountered such a requirement, is to generate a local PDF file, and then open locally, after testing that the PDF file can not be saved to the album, can only be stored in the phone, in view of Apple's storage mechanism, need to take out, for local display, you can directly upload to the background to generate a link, Directly in the WebView and browser open, but the effect is not good, loading is also slow, so think of using the Uidocumentinteractioncontroller to achieve local view of the operation, the specific code is as follows:

1, Introduction

Uidocumentinteractioncontroller is supported from the SDK of iOS 3.2, he is directly inherited from NSObject, so needs uidocumentinteractioncontroller to provide a way to show him, Uidocumentinteractioncontroller mainly provides us with three uses:

1 show a list of third-party apps that can manipulate the types of documents we share 2 add additional actions based on the first presentation list, such as copy, print, preview, save, etc. 3 displaying document content directly with the quick look frame

2, the simple layout will not say, directly on the main code:

Initialization

-(Ibaction) Shareclick: (ID) sender {    *documentcontroller = [Uidocumentinteractioncontroller Interactioncontrollerwithurl:[[nsbundle Mainbundle] Urlforresource:@ "MyFile" Withextension:@ "PDF"];}

To implement a Uidocumentinteractioncontroller method:

-(BOOL) Presentopeninmenufromrect: (cgrect) rect inview: (UIView *) View animated: (bool) animated;

Add this method in the click Method of the button above:

Uidocumentinteractioncontroller *documentcontroller = [Uidocumentinteractioncontroller Interactioncontrollerwithurl:[[nsbundle Mainbundle] Urlforresource:@ "MyFile" Withextension:@ "PDF"];    [Documentcontroller presentOpenInMenuFromRect:self.view.bounds InView:self.view Animated:yes];

Then run the project, test, click on the button, you can see the following interface:

Here I do the local Chinese processing, the real machine will display Chinese characters, specifically, please refer to my "System control Chinese" blog.

A brief introduction to the following interface:

The first list shows that "AirDrop" is a iOS 7 technology that Apple offers to share across devices;

The second list shows the entire iOS system, an app listing that can manipulate PDF documents, and also includes an Apple iOS 8 -provided Share Extension icon;

The third line of the table shows the practical equipment optional operation, such as Copy , Print etc.;

This time, click Share to, this time, will find crashes, error messages are as follows:

According to the error, "***has gone away prematurely" premature disappearance, in the ARC environment, the method after the completion UIDocumentInteractionController of the instance was released, the display of this view is a quick look frame to operate, not to UIDocumentInteractionController produce a reference, So when you click on the button, the internal operation will still continue to access the instance, so it will be reported premature disappearance of this error!

WORKAROUND: Declare an UIDocumentInteractionController instance of the strong type as a strong pointer, and then modify the trigger method for the Next button:

@property (Nonatomic,strong) Uidocumentinteractioncontroller *documentcontroller;

Method is written as a private method:

-(void) Presentopeninmenu
{
[_documentcontroller presentOpenInMenuFromRect:self.view.bounds InView:self.view Animated:yes];
}

Final code:

-(Ibaction) Shareclick: (ID) sender {    = [Uidocumentinteractioncontroller Interactioncontrollerwithurl:[[nsbundle Mainbundle] Urlforresource:@ "MyFile" Withextension:@ "PDF"];    presentopeninmenu];}

This will solve!

Show optional actions to implement this method:

-(BOOL) Presentoptionsmenufromrect: (cgrect) rect inview: (UIView *) View animated: (bool) animated;

Encapsulating Private Methods:

-(void) presentoptionsmenus{    [_documentcontroller presentOptionsMenuFromRect:self.view.bounds InView:self.view Animated:yes];}

Then in the button click Method to replace the above method, the effect such as:

Gorgeous Lai's appeared!

Direct preview

UIDocumentInteractionControllerThe third way to preview the content of the document is to focus, but also very common, such as inside and inside the browser is the use of this method, is very convenient.

It is also convenient to implement a preview, implementing a proxy method:

-(Uiviewcontroller *) Documentinteractioncontrollerviewcontrollerforpreview: (Uidocumentinteractioncontroller *) Controller

This proxy method is primarily used to specify the UIDocumentInteractionController parent view of the view to be displayed, so that you UIDocumentInteractionController know where to show the quick look preview, and of course, here is the proxy object for the VC that specifies the button UIDocumentInteractionController , adding the following code:

@interface Viewcontroller () <UIDocumentInteractionControllerDelegate>
_documentcontroller. delegate = self;

To implement the Proxy method:

-(Uiviewcontroller *) Documentinteractioncontrollerviewcontrollerforpreview: (Uidocumentinteractioncontroller * ) controller{    return self ;}

UIDocumentInteractionControlleris inherited NSObject , so in order to be able to achieve a direct preview, we need to use the UIDocumentInteractionController provided display preview method:

-(BOOL) presentpreviewanimated: (bool) animated;

Private methods:

-(void) presentpreview{    [Self.documentcontroller presentpreviewanimated:yes];}

Add this method to the button click event and the effect is as follows:

This shows the PDF file, on the gorgeous show out!

Reference from: http://www.jianshu.com/p/3f03897cf98a

Use of iOS development--uidocumentinteractioncontroller (for more sharing services)

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.