iOS to open or preview documents with Uidocumentinteractioncontroller and quick look

Source: Internet
Author: User

In the development of the app, we can not avoid to open files in the software, such as: Excel files, Word files, picture files and other files in different formats, or want to use third-party apps to open these files, Then we need to use Uidocumentinteractioncontroller and quick look to solve these problems.
    • How to use Uidocumentinteractioncontroller

    • Uidocumentinterractioncontroller precautions when using the

    • How to use Quick look

first, the use of Uidocumentinteractioncontroller method
- first create a Uidocumentinteractioncontroller object and initialize a URL to the object as the file path

1, first to follow the Uidocumentinteractioncontrollerdelegate2, followed by the creation of a Uidocumentinteractioncontroller object@property(nonatomic, retain) Uidocumentinteractioncontroller *doccontroller;3, the initialization of the Uidocumentinteractioncontroller object in the Method-(void) open{NSString*filename = [ Self. Listdicobjectforkey:@"FileName"];NSString* Path = [Nshomedirectory () Stringbyappendingpathcomponent:_doccontroller = [Uidocumentinteractioncontroller interactioncontrollerwithurl:[NsurlFileurlwithpath:path]];//Initialize a load path for the object_doccontroller. Delegate= Self;//Set up proxy//Show preview directly//[_doccontroller Presentpreviewanimated:yes];CGRectNavrect = Self. Navigationcontroller. Navigationbar. Frame; Navrect. Size=cgsizemake (1500.0F40.0f);//Show menu items that contain previews[_doccontroller Presentoptionsmenufromrect:navrectinview: Self. viewanimated:YES];//display does not include preview menu items//[doccontroller presentopeninmenufromrect:navrect InView:self.view Animated:yes];}4, Proxy Method-(Uiviewcontroller*) Documentinteractioncontrollerviewcontrollerforpreview: (Uidocumentinteractioncontroller *) controller{return  Self;} - (UIView*) Documentinteractioncontrollerviewforpreview: (Uidocumentinteractioncontroller *) controller{return  Self. View;} - (CGRect) Documentinteractioncontrollerrectforpreview: (Uidocumentinteractioncontroller *) controller{return   Self. View. Frame;}

Second, the use of Uidocumentinterractioncontroller precautions

1, when the definition of Uidocumentinterractioncontroller must be retain type, because it is necessary to hold the object;

2. When you select Show menu items that contain previews
[_doccontroller PresentOptionsMenuFromRect:navRectinView:self.viewanimated:YES]; it should be noted that this method triggers the built-in printout of the class, The log information will be printed out, causing the app to crash seriously and may cause the phone to panic, but no workaround has been found. The output is similar to the following (omit the later part of the output, because there are too many):

unknown activity items supplied: (        {        "public.jpeg"=<00104494600010101006000600000000302020302020303030304030304050805050404050......

When the above method does not meet your requirements, you can consider using quick look.

Iii. How to use Quick look
1, the first to add quicklook.framework framework, specifically how to add I will not explain.
2. Add a header file to the controller you need to use #import

- (void)open{        QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];        myQlPreViewController.delegate =self;      myQlPreViewController.dataSource =self;      [myQlPreViewController setCurrentPreviewItemIndex:0];      [self presentViewController:myQlPreViewControlleranimated:YEScompletion:nil];

5. Agent method

-(NSInteger  ) Numberofpreviewitemsinpreviewcontroller: (Qlpreviewcontroller *) controller{return  1 ;} -(id  <QLPreviewItem>) Previewcontroller: (Qlpreviewcontroller *) controller Previewitematindex: (nsinteger ) index{NSString  *filename = [self  .listdicobjectforkey    : @ "FileName" ]; nsstring  * path = [Nshomedirectory () stringbyappendingpathcomponent:[    Nsstringstringwithformat:@ "documents/%@" , FileName]]; return  [Nsurlfileurlwithpath:path];}  

iOS to open or preview documents using Uidocumentinteractioncontroller and quick look

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.