寻求iOS应用的一些帮助。在我的应用程序中,我想在WKWebView中打开我的域内的点击链接(EX:communionchapelefca.org),然后在Safari中打开所有其他域(EX:google.com)的链接。我更喜欢以程序方式执行此操作,因为这是我的代码已经设置的方式。
我在stackoverflow上找到了一些解决方案(here,here,here和here),但它们似乎都是基于Obj-C的,我是使用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)
答案 0 :(得分:79)
您可以实施WKNavigationDelegate
,添加decidePolicyForNavigationAction
方法并检查navigationType和请求的网址。我在下方使用了 google.com ,但您可以将其更改为您的域名:
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)
}
}
}
答案 1 :(得分:8)
对于Swift 3.0
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
let wv = WKWebView(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
guard let url = NSURL(string: "https://www.google.com") else { return }
wv.navigationDelegate = self
wv.load(NSURLRequest(url: url as URL) as URLRequest)
view.addSubview(wv)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .LinkActivated {
if let newURL = navigationAction.request.url,
let host = newURL.host , !host.hasPrefix("www.google.com") &&
UIApplication.shared.canOpenURL(newURL) &&
UIApplication.shared.openURL(newURL) {
print(newURL)
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)
}
}
}
答案 2 :(得分:5)
以下是来自obj c中写的swift响应的示例代码。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler
{
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
if (navigationAction.request.URL) {
NSLog(@"%@", navigationAction.request.URL.host);
if (![navigationAction.request.URL.resourceSpecifier containsString:@"ex path"]) {
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
decisionHandler(WKNavigationActionPolicyCancel);
}
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
答案 3 :(得分:2)
我的快速3解决方案:
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
if url?.description.lowercased().range(of: "http://") != nil || url?.description.lowercased().range(of: "https://") != nil {
decisionHandler(.cancel)
UIApplication.shared.openURL(url!)
} else {
decisionHandler(.allow)
}
}
不要忘记设置te委托
public override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
答案 4 :(得分:1)
创建一个函数来决定加载URL的位置:
func loadURLString(str: String) {
guard let url = NSURL(string: str) else {
return
}
if url.host == "www.communionchapelefca.org" {
// Open in myWebView
myWebView.loadRequest(NSURLRequest(URL: url))
} else {
// Open in Safari
UIApplication.sharedApplication().openURL(url)
}
}
用法:
loadURLString("http://www.communionchapelefca.org/app-home") // Open in myWebView
loadURLString("http://www.apple.com") // Open in Safari
答案 5 :(得分:0)
George Vardikos答案的Swift 4更新:
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
guard url != nil else {
decisionHandler(.allow)
return
}
if url!.description.lowercased().starts(with: "http://") ||
url!.description.lowercased().starts(with: "https://") {
decisionHandler(.cancel)
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
decisionHandler(.allow)
}
}
答案 6 :(得分:0)
我的 2 美分用于 swift 5 和 iOS >10 ( UIApplication.shared.openURL(url!) 已弃用)
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else{
decisionHandler(.allow)
return
}
let urlString = url.absoluteString.lowercased()
if urlString.starts(with: "http://") || urlString.starts(with: "https://") {
decisionHandler(.cancel)
UIApplication.shared.open(url, options: [:])
} else {
decisionHandler(.allow)
}
}
答案 7 :(得分:-1)
Xcode 9.3,Swift 4
来源: 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)
}}