해당 문제에 대한 해결책을 찾으려면 일주일 이상 테스트 한 후 확인하십시오.
여기 해결책이 있습니다. 사용자가 웹보기 안의 앱에서 클릭하면 발생하는 일을 처리하기 위해 작성한 코드가 중요하기 때문에 전화 구조는 중요하지 않습니다. 그 책임이있는 클래스에서
(보기 컨트롤러)도
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