IOSとWKWebviewsを使用してtarget="_blank"
を持つリンクを開く、またはURLにhttp://
、https://
、またはmailto:
が含まれているハイブリッドSwiftアプリを取得しようとしていますモバイルSafariで。
この回答 からこのコードを取得します。
func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
if navigationAction.targetFrame == nil {
webView.loadRequest(navigationAction.request)
}
return nil
}
まず、それは私には何の役にも立ちません。次に、新しいウィンドウで開きたいです。そして、私はそのようなことをすることになっているこのコードを見つけました...
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.sharedApplication().openURL(requestUrl)
}
これら2つを組み合わせて機能させるにはどうすればよいですか? ViewController宣言を機能させるには、何を追加する必要がありますか?
IOS10用に更新されたコードSwift 3:
override func loadView() {
super.loadView()
self.webView.navigationDelegate = self
self.webView.uiDelegate = self //must have this
}
func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil, let url = navigationAction.request.url {
if url.description.lowercased().range(of: "http://") != nil ||
url.description.lowercased().range(of: "https://") != nil ||
url.description.lowercased().range(of: "mailto:") != nil {
UIApplication.shared.openURL(url)
}
}
return nil
}
で( ここから )
override func loadView() {
super.loadView()
self.webView.navigationDelegate = self
self.webView.UIDelegate = self //must have this
}
次に、関数を追加します( ここから、追加あり ).。
func webView(webView: WKWebView,
createWebViewWithConfiguration configuration: WKWebViewConfiguration,
forNavigationAction navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
var url = navigationAction.request.URL
if url.description.lowercaseString.rangeOfString("http://") != nil || url.description.lowercaseString.rangeOfString("https://") != nil || url.description.lowercaseString.rangeOfString("mailto:") != nil {
UIApplication.sharedApplication().openURL(url)
}
}
return nil
}
最初にWKNavigationDelegate
とwebviewWk.navigationDelegate = self
を追加します
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
//this is a 'new window action' (aka target="_blank") > open this URL externally. If we´re doing nothing here, WKWebView will also just do nothing. Maybe this will change in a later stage of the iOS 8 Beta
if navigationAction.navigationType == WKNavigationType.LinkActivated {
println("here link Activated!!!")
let url = navigationAction.request.URL
let shared = UIApplication.sharedApplication()
let urlString = url!.absoluteString
if shared.canOpenURL(url!) {
shared.openURL(url!)
}
decisionHandler(WKNavigationActionPolicy.Cancel)
}
decisionHandler(WKNavigationActionPolicy.Allow)
}
func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil, let url = navigationAction.request.url, let scheme = url.scheme {
if ["http", "https", "mailto"].contains(where: { $0.caseInsensitiveCompare(scheme) == .orderedSame }) {
UIApplication.shared.openURL(url)
}
}
return nil
}
Swift 4.2
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
if navigationAction.navigationType == WKNavigationType.linkActivated {
print("here link Activated!!!")
if let url = navigationAction.request.url {
let shared = UIApplication.shared
if shared.canOpenURL(url) {
shared.open(url, options: [:], completionHandler: nil)
}
}
decisionHandler(.cancel)
}
else {
decisionHandler(.allow)
}
}