WKWebView 在 safari 中打开来自某个域的链接 [英] WKWebView open links from certain domain in safari

查看:38
本文介绍了WKWebView 在 safari 中打开来自某个域的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在 WKWebView 中打开来自 my 域(例如:communionchapelefca.org)内的链接,但随后在 Safari 中打开来自 其他 域的链接.我更愿意以编程方式执行此操作.

Within my app I am want to open links from within my domain (e.g.: communionchapelefca.org) in WKWebView but then have links from other domains open in Safari. I would prefer to do this programmatically.

我找到了一些关于堆栈溢出的解决方案(此处这里此处here) 但它们似乎都是基于 Obj-C 的,我正在寻找使用 Swift 的解决方案.

I have found a few solutions on Stack overflow (here, here, here, and here) but they all seem to be Obj-C based and I am looking for a solution using Swift.

ViewController.swift:

ViewController.swift:

import UIKit
import WebKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myWebView:WKWebView = WKWebView(frame: CGRectMake(0, 0,   UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
        
        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.communionchapelefca.org/app-home")!))
        
        self.view.addSubview(myWebView)

推荐答案

您可以实现 WKNavigationDelegate,添加 decidePolicyForNavigationAction 方法并检查导航类型和请求的 url.我在下面使用了 google.com,但您可以将其更改为您的域:

You can implement WKNavigationDelegate, add the decidePolicyForNavigationAction method and check there the navigationType and requested url. I have used google.com below but you can just change it to your domain:

Xcode 8.3 • Swift 3.1 或更高版本

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    let webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()

        webView.frame = view.bounds
        webView.navigationDelegate = self

        let url = URL(string: "https://www.google.com")!
        let urlRequest = URLRequest(url: url)

        webView.load(urlRequest)
        webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        view.addSubview(webView)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated  {
            if let url = navigationAction.request.url,
                let host = url.host, !host.hasPrefix("www.google.com"),
                UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
                print(url)
                print("Redirected to browser. No need to open it locally")
                decisionHandler(.cancel)
            } else {
                print("Open it locally")
                decisionHandler(.allow)
            }
        } else {
            print("not a user click")
            decisionHandler(.allow)
        }
    }
}

这篇关于WKWebView 在 safari 中打开来自某个域的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆