WKWebView open links from certain domain in safari

2020-01-24 04:05发布

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.

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:

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)

7条回答
再贱就再见
2楼-- · 2020-01-24 05:08

Xcode 9.3, Swift 4

The source: https://developer.apple.com/documentation/webkit/wkwebview

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()

    let myURL = URL(string: "https://www.apple.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}}
查看更多
登录 后发表回答