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

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

问题描述

寻求iOS应用的一些帮助。在我的应用程序中,我想在WKWebView中打开我的域内的点击链接(EX:communionchapelefca.org),然后在Safari中打开所有其他域(EX:google.com)的链接。我更喜欢以程序方式执行此操作,因为这是我的代码已经设置的方式。

Looking for some help with my iOS app. Within my app I am wanting to open clicked links from within my domain (EX: communionchapelefca.org) in WKWebView and then have links from all other domains (EX: google.com) open in Safari. I would prefer to do this progrmatically as well since that is how my code is already setup.

我在stackoverflow上找到了一些解决方案(这里这里此处,以及这里)但它们似乎都是基于Obj-C的,我正在寻找使用Swift的解决方案。提前谢谢。

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

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 ,添加 decisionPolicyForNavigationAction 方法并检查navigationType和请求的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()
        guard let url = URL(string: "https://www.google.com") else { return }
        webView.frame = view.bounds
        webView.navigationDelegate = self
        webView.load(URLRequest(url: url))
        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天全站免登陆