ios在項目中打開word文檔、ppt等總結

最近在項目開發中遇到下載附件文檔預覽需求,在這裏總結一下個人實現方法,本文最後會附帶我寫的demo下載地址git

這裏我總結了三種實現方法(1)用webView預覽(2)經過UIDocumentInteractionController實現跳轉(3)應用Quick Look系統框架,下面依次介紹各個方法實現github

首先來看用webView這個比較經常使用,不作過多解釋,代碼以下:web

_webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    _webView.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:_url1];
    [_webView loadRequest:request];
    [_webView setScalesPageToFit:YES];
    [self.view addSubview:_webView];

 第二種應用UIDocumentInteractionController實現方法以下:框架

//先初始化對象,以及設置彈出方式
_documentInt = [UIDocumentInteractionController interactionControllerWithURL:_url2];
    [_documentInt setDelegate:self];
    [_documentInt presentPreviewAnimated:YES];
    [_documentInt presentOptionsMenuFromRect:CGRectMake(0, 0, 375, 667) inView:self.view  animated:YES];
//而後實現相應代理方法
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
     return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
     return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    
     return self.view.frame;
}
//點擊預覽窗口的「Done」(完成)按鈕時調用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
    [_documentInt dismissPreviewAnimated:YES];
}

 第三種Quick Look,用這個方法須要導入QuickLook.FrameWork框架,代碼以下:ui

//初始化對象
QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];
    myQlPreViewController.delegate =self;
    myQlPreViewController.dataSource =self;
    [myQlPreViewController setCurrentPreviewItemIndex:0];
    [self presentViewController:myQlPreViewController animated:YES completion:nil];
//實現代理方法
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    //    NSString *fileName = [self.listDic objectForKey:@"fileName"];
    //    NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@"Documents/%@",fileName]];
    
    return _url3;
    
}

 demo下載地址:https://github.com/zk1947/ZKDemoALLurl

相關文章
相關標籤/搜索