美文网首页iOS表格绘制
iOS之WKWebview加载下载PDF的URL注意点

iOS之WKWebview加载下载PDF的URL注意点

作者: LiuffSunny | 来源:发表于2021-01-29 16:07 被阅读0次

业务前景,后台返回特定url,如果在浏览器打开是这样的,允许下载,下载下来是pdf格式


image.png

问题一: 遇到http和https的错误

控制台打印信息

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <FCF47A78-B88D-480E-B937-BE9ADE584CA5>.<2>, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

解决方案:iOS安全性要求https,可以暂时关掉。
在info.plist文件里Htttps 和Http 设置如下


image.png

问题二 加载WKWebView 显示空白

很奇怪的遇到了这个问题

image.png
解决方案,加入代理方法
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{

    NSLog(@"%@",navigationResponse.response.URL.absoluteString);
    //允许跳转
    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{

     NSLog(@"%@",navigationAction.request.URL.absoluteString);
    //允许跳转
    decisionHandler(WKNavigationActionPolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationActionPolicyCancel);
}

问题3 加载出来乱码

image.png
解决方案
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSLog(@"%@",navigationResponse.response.URL.absoluteString);
    NSString *urlString = @"你最初加载的URL";
// 拿到二进制
    NSData *data = [NSData dataWithContentsOfURL:navigationResponse.response.URL];
    NSURL *weburl = [NSURL URLWithString:urlString];
    [self.webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"UTF-8" baseURL:weburl];
    //允许跳转
    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}

圆满解决

image.png

所有代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.webView];
    // 替换为自己后台给的url 你最初加载的URL
    NSString *urlString = @"http://your.service.com"; 
    NSURL *weburl = [NSURL URLWithString:urlString];
    NSURLRequest *request;
    request = [NSURLRequest requestWithURL:weburl];
    [self.webView loadRequest:request];
}
// 页面准备加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"------页面准备加载时调用");
}
// 页面内容开始加载
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"------页面内容开始加载");
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"------页面,%@",error);
//    if (error.code = 102) {
//        [webView reload];
//    }
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"------页面,%@",error);
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSLog(@"%@",navigationResponse.response.URL.absoluteString);
    NSString *urlString = @"你最初加载的URL";
    NSData *data = [NSData dataWithContentsOfURL:navigationResponse.response.URL];
    NSURL *weburl = [NSURL URLWithString:urlString];
    [self.webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"UTF-8" baseURL:weburl];
    //允许跳转
    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
     NSLog(@"%@",navigationAction.request.URL.absoluteString);
    //允许跳转
    decisionHandler(WKNavigationActionPolicyAllow);
    //不允许跳转
//    decisionHandler(WKNavigationActionPolicyCancel);
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {

    // 禁用用户选择
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';"completionHandler:nil];
     [self.webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';" completionHandler:nil];
}
- (WKWebView *)webView {
    if (_webView == nil) {
        WKWebViewConfiguration *webConfiguration = [WKWebViewConfiguration new];
        webConfiguration.allowsInlineMediaPlayback = YES;//是否允许内联(YES)或使用本机全屏控制器(NO),默认是NO。
//        webConfiguration.mediaPlaybackAllowsAirPlay = YES;//允许播放,ios(8.0, 9.0)
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:webConfiguration];
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
    }
    return _webView;
}

相关文章

网友评论

    本文标题:iOS之WKWebview加载下载PDF的URL注意点

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