I want to launch Safari from WKWebView

Asked 2 years ago, Updated 2 years ago, 36 views

We are currently in the process of migrating from UIWebview to WKWebview.
However, the behavior of checking PDFs in Safari, not in the app, stopped working in UIWebview.

The following is the source of the problem:
My language is Swift.

 func webView (webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction!, windowFeatures: WKWindowFeatures!) - > WebView!

    if navigationAction.navigationType==WKNavigationType.FormSubmitted {
            let pattern = "URL in PDF"
            let flag = Regexp(pattern).isMatch(String(navigationAction.request.URL!))
            print(flag)
            print(navigationAction.request.URL!)
            if flag{
                UIAApplication.sharedApplication().openURL(navigationAction.request.URL!);
            }
        }

        if navigationAction.navigationType==WKNavigationType.LinkActivated{
            let pattern = "URL in PDF"
            let flag = Regexp(pattern).isMatch(String(navigationAction.request.URL!))
            print(flag)
            print(navigationAction.request.URL!)
            if flag{
                UIAApplication.sharedApplication().openURL(navigationAction.request.URL!);
            }
        }

        return nil
    }

import Foundation

class Regexp {
    let internalRegexp:NSRegularExpression
    let pattern:string

    init(_pattern:String){
        self.pattern = pattern
        self.internalRegexp=try!NSRegularExpression (pattern:pattern, options:NSRegularExpressionOptions.CaseInsensitive)
    }

    funcisMatch(input:String) - >Bool{
        let matches = self.internalRegexp.matchesInString(input, options:[], range:NSMakeRange(0, input.characters.count))
        return matches.count > 0
    }

    func matches (input:String) - > [String] ?{
        if self.isMatch(input){
            let matches = self.internalRegexp.matchesInString(input, options:[], range:NSMakeRange(0, input.characters.count))
            var results: String = [ ]
            for in 0..<matches.count {
                results.append((input as NSSstring).substringWithRange(matches[i].range))
            }
            return results
        }
        return nil
    }

Here's the pre-migration source.

 func webView (webView:UIWebView!, shouldStartLoadWithRequest request:NSURLRequest!, navigationType:UIWebViewNavigationType) ->Bool{

        if navigationType==UIWebViewNavigationType.FormSubmitted{
            let pattern = "URL in PDF"
            let flag = Regexp(pattern).isMatch(String(request.URL!))
            print(flag)
            print(request.URL)
            if flag{
                UIAApplication.sharedApplication().openURL(request.URL!);
                return false;
            }
        }

        if navigationType==UIWebViewNavigationType.LinkClicked{
            let pattern = "URL in PDF"
            let flag = Regexp(pattern).isMatch(String(request.URL!))
            print(flag)
            print(request.URL)
            if flag{
                UIAApplication.sharedApplication().openURL(request.URL!);
                return false;
            }
        }

        return true
    }

Currently, PDFs are displayed in Sebview, not Safari.
Is there anything wrong with the description?

I'm sorry, but if you know, please reply.

swift ios

2022-09-30 19:40

1 Answers

Please describe the Swift version (probably 2 series)

Here's the answer, uiDelegate's

 func webView (webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction!, windowFeatures: WKWindowFeatures!) - > WebView!

}

instead of

navigationDelegate

func webView (webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy)->Void){

}

Why don't you take care of it?

Here are the sources we experimented with.
The conditions are different, but PDF files should be opened in safari.

import UIKit
import WebKit

classViewController:UIViewController, WKNavigationDelegate {

    var webView —WKWebView!

    override func viewDidLoad(){
        super.viewDidLoad()

        webView = WKWebView (frame: UISscreen.mainScreen().bound)
        webView.navigationDelegate=self
        view.addSubview (webView)

        if let url = NSURL(string: "https://google.co.jp") {
            webView.loadRequest (NSURLRequest (URL:url))
        }
    }

    func webView (webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy)->Void){

        iflet url=navigationAction.request.URL where url.pathExtension?.lowercaseString=="pdf"{
            decisionHandler (.Cancel)
            UIAApplication.sharedApplication().openURL(url)
        } else{
            decisionHandler (.Allow)
        }
    }
}

Try it.


2022-09-30 19:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.