美文网首页
iOS 预览word pdf 文件

iOS 预览word pdf 文件

作者: 龍飝 | 来源:发表于2019-03-21 13:57 被阅读0次
    此类用于改变QLPreviewController 导航栏title
    
    #import <QuickLook/QuickLook.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface QLPreviewController (title)
    
    @property (nonatomic, strong) NSString *qlpTitle;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "QLPreviewController+title.h"
    #import <objc/runtime.h>
    
    @implementation QLPreviewController (title)
    
    - (void)setQlpTitle:(NSString *)qlpTitle {
        objc_setAssociatedObject(self, @"qlpTitle", qlpTitle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSString *)qlpTitle {
        return objc_getAssociatedObject(self, @"qlpTitle");
    }
    
    - (void)setTitle:(NSString *)title {
        if (self.qlpTitle) {
            self.navigationItem.title = self.qlpTitle;
        } else {
            self.navigationItem.title = title;
        }
    }
    
    @end
    
    预览效果
    
    引入#import <QuickLook/QuickLook.h>
    
    遵循代理 QLPreviewControllerDataSource,QLPreviewControllerDelegate  定义UIWebView *openFileWebView;
    

    点击效果代码

    openFileWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
        openFileWebView.delegate = self;
        [openFileWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.workDetailsModel.annex]]];
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        return YES;
    }
    #pragma mark - Web代理
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
       
            NSURL *targetURL = [NSURL URLWithString:self.workDetailsModel.annex];
            
            NSString *docPath = [self documentsDirectoryPath];
            NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, [targetURL lastPathComponent]];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            BOOL hasDownLoad= [fileManager fileExistsAtPath:pathToDownloadTo];
            if (hasDownLoad) {
                self.fileURL = [NSURL fileURLWithPath:pathToDownloadTo];
                QLPreviewController *qlVC = [[QLPreviewController alloc]init];
                qlVC.delegate = self;
                qlVC.dataSource = self;
                //            qlVC.tabBarItem.title = @"预览附件";
                qlVC.navigationItem.hidesBackButton = YES;
                qlVC.qlpTitle = @"预览附件";
                [self.navigationController pushViewController:qlVC animated:YES];
                [WProgressHUD hideAllHUDAnimated:YES];
                //
            }
            else {
                NSURL *targetURL = [NSURL URLWithString:self.workDetailsModel.annex];
                
                NSData *fileData = [[NSData alloc] initWithContentsOfURL:targetURL];
                // Get the path to the App's Documents directory
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
                [fileData writeToFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, [targetURL lastPathComponent]] atomically:YES];
                NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
                [openFileWebView loadRequest:request];
                
            }
        
        NSLog(@"webViewDidFinishLoad");
    }
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        NSLog(@"didFailLoadWithError");
        
        
    }
    
    - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
        return 1;
    }
    
    - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
        return self.fileURL;
    }
    
    - (void)previewControllerWillDismiss:(QLPreviewController *)controller {
        NSLog(@"previewControllerWillDismiss");
    }
    
    - (void)previewControllerDidDismiss:(QLPreviewController *)controller {
        NSLog(@"previewControllerDidDismiss");
    }
    
    - (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item{
        return YES;
    }
    
    - (CGRect)previewController:(QLPreviewController *)controller frameForPreviewItem:(id <QLPreviewItem>)item inSourceView:(UIView * __nullable * __nonnull)view{
        return CGRectZero;
    }
    

    相关文章

      网友评论

          本文标题:iOS 预览word pdf 文件

          本文链接:https://www.haomeiwen.com/subject/dfvqvqtx.html