ios预览在线pdf

最近公司用vue做跨平台前端,然后直接用UIWebView封装成app。体验还不错。

但是在预览附件的时候遇到一些问题:

1. 在页面中点击jpg文件可以正常预览,预览完成后可以关闭返回主页面;

2. 但是对于在线pdf文件,虽然可以正常预览,但预览的时候直接最大化窗口,无法关闭,所以也无法返回主界面,双击pdf预览窗口也无反应。


1. 首先需要解决预览在线pdf;

 @property (weak, nonatomic) IBOutlet UIWebView *web;
    
    NSURL *URL = [NSURL fileURLWithPath:@"http://wwwfujian.book118.com//dianzishu/%E5%90%8C%E5%9F%8E%E7%BD%91/3%E6%9C%88%E5%88%9D_83402/pdf/%E5%90%8C%E7%A8%8B%E7%BD%91%E5%BC%80%E5%B9%B3%E6%97%85%E6%B8%B8%E6%94%BB%E7%95%A5%EF%BC%882012%E5%B9%B4%E7%89%88%EF%BC%89.pdf"];

    if (URL) {
        // Initialize Document Interaction Controller
        self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
        
        // Configure Document Interaction Controller
        [self.docInteractionController setDelegate:self];
        
        
        self.docInteractionController.UTI = @"com.adobe.pdf";
        
        // Present Open In Menu
        //[self.docInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
        
        [self.docInteractionController presentPreviewAnimated:YES];
    }


网上找了很多,都是类似的方法,却怎么都不行。


尝试改为本地pdf,直接就可以了,说明方法是对的。

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Steve"
                                                         ofType:@"pdf"];


下一步需要考虑如何把在线url形式的pdf转换为本地文件,即通过下载到本地。

   // Build URLS
    NSURL* url = [[NSURL alloc] initWithString:@"http://192.168.3.2:86/wap/file/downloadFile?fileId=9c2b408263dc4135a29eac8a1a1a0cec&fileUri=58e6feba0cf2532a21b0a249&fileName=abctest.pdf"];
    if (url) {
        NSURL* documentsUrl = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
        NSURL* destinationUrl = [documentsUrl URLByAppendingPathComponent:@"temp.pdf"];
        
        // Actually download
        NSError* err = nil;
        NSData* fileData = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingUncached error:&err];
        if (!err && fileData && fileData.length && [fileData writeToURL:destinationUrl atomically:true])
        {
  
            // Initialize Document Interaction Controller
            self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:destinationUrl];
            
            // Configure Document Interaction Controller
            [self.docInteractionController setDelegate:self];
            
            
            self.docInteractionController.UTI = @"com.adobe.pdf";
            
            // Present Open In Menu
            //[self.docInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
            
            [self.docInteractionController presentPreviewAnimated:YES];
        }
    }


通过上面的代码就可以实现预览在线pdf了,但是发现有一些pdf并不可以,获取到的NSURL是空的,怀疑是url中有中文字符导致,于是将url进行了编码处理。

            NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
            NSString *encodeUrl = [absolutePath stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
            NSLog(@"%@", encodeUrl);


测试没问题以后,开始下一步。


2. 成功后,才能通过协议拦截,自定义pdf查看;

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    //NSLog(@"%@", NSStringFromSelector(_cmd));
    
    //OC调用JS是基于协议拦截实现的 下面是相关操作
    NSString *absolutePath = request.URL.absoluteString;
    if (absolutePath != nil) {
        NSRange range = [absolutePath rangeOfString:@".pdf"];
        if (range.location != NSNotFound) {
            NSLog(@"%@", absolutePath);
            
            NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
            NSString *encodeUrl = [absolutePath stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
            NSLog(@"%@", encodeUrl);
            
            NSURL* url = [[NSURL alloc] initWithString:encodeUrl];
            if (url) {
                NSURL* documentsUrl = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
                NSURL* destinationUrl = [documentsUrl URLByAppendingPathComponent:@"temp.pdf"];
                
                // Actually download
                NSError* err = nil;
                NSData* fileData = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingUncached error:&err];
                if (!err && fileData && fileData.length && [fileData writeToURL:destinationUrl atomically:true])
                {

                    // Initialize Document Interaction Controller
                    self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:destinationUrl];
                    
                    // Configure Document Interaction Controller
                    [self.docInteractionController setDelegate:self];
                    
                    self.docInteractionController.UTI = @"com.adobe.pdf";
                    
                    [self.docInteractionController presentPreviewAnimated:YES];
                    
                    return NO;
                }
            }
        }
    }

    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{

}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"--------------%@",error);
}


发现点击以后,没有显示url,更谈不上拦截,检查已经设置了

webView.delegate = self;

也定义了:

@interface ViewController ()<UIWebViewDelegate, UIDocumentInteractionControllerDelegate>


那么问题在那?翻看以前的项目,应该区别在于webView创建的方式了,当前是动态创建,改为了绑定的方式后,一切正常。

@property (weak, nonatomic) IBOutlet UIWebView *web;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值