問題の解決策を見つけるために、1週間以上テストしてから問題ないです。
これが解決策です。ユーザーが(Webビュー内のアプリケーションから)クリックすると、何が起こるかを処理するために書いたコードを重要視しているので、電話の構造にはまったく問題はありませんでした。その責任クラスで
(あなたのビューコントローラ)も
extension ViewController: WKNavigationDelegate, WKUIDelegate {
// handle it here
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// print(navigationAction.request.url!)
// if the URL have tel in it then use the iOS to open it not the webkit
if (navigationAction.request.url!.absoluteString.contains("tel")) {
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
} else if (!(navigationAction.request.url!.absoluteString.contains(MAINURLNOTTOGOOUTSIDE))) {
// if the URL is something outside of the one specified in that string "MAINURLNOTTOGOOUTSIDE", open it using the iOS not the webkit
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let app = UIApplication.shared
if (navigationAction.targetFrame == nil) {
if (navigationAction.request.url!.scheme?.contains("tel"))! {
if (app.canOpenURL(navigationAction.request.url!)) {
app.openURL(navigationAction.request.url!)
}
}
}
return nil
}
// handle if failed to connect to the server
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
var message = ""
var title = ""
var button = ""
if (Locale.preferredLanguages[0].contains("ar")) {
title = "خطأ"
message = "تعذر الوصول الى الخادم الرجاء المحاولة في وقت لاحق"
button = "حسنا"
} else {
title = "Error"
message = "Unable to access server Please try again later"
button = "OK!"
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: button, style: .default, handler: nil))
}
// handle if failed to connect to the server
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
var message = ""
var title = ""
var button = ""
if (Locale.preferredLanguages[0].contains("ar")) {
title = "خطأ"
message = "تعذر الوصول الى الخادم الرجاء المحاولة في وقت لاحق"
button = "حسنا"
} else {
title = "Error"
message = "Unable to access server Please try again later"
button = "OK!"
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: button, style: .default, handler: nil))
}
}
はviewDidLoadメソッドでは、次の含めることを忘れないでください
self.WebView.navigationDelegate = self