单击 webkit 上的操作标签时如何打开调用对话框?
how to open call dialog when click on action tag on webkit?
我在我的应用程序中使用了 webkit。
如图所示,“继续”和呼叫按钮(phone 图标)都是来自网站的操作标签。
当我点击“继续”时,它会让我进入下一页,但是当我点击呼叫按钮(phone 图标)时,没有任何反应。
我想在 ios 中单击呼叫按钮(phone 图标)时打开呼叫拨号器。
我所做的如下,
我的代码是
import UIKit
import WebKit
import Alamofire
class ContinueViewController: UIViewController, WKUIDelegate {
func getPostString(params:[String:Any]) -> String{
var data = [String]()
for(key, value) in params
{
data.append(key + "=\(value)")
}
return data.map { String([=10=]) }.joined(separator: "&")
}
@IBOutlet weak var activityView: UIActivityIndicatorView!
@IBOutlet weak var webview: WKWebView!
var url: URL?
var id = 0, skill = 0
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 14.0, *) {
webview.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
}else {
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
configuration.preferences = preferences
}
webview.navigationDelegate = self
self.navigationController?.navigationBar.isHidden = true
webview.uiDelegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
activityView.startAnimating()
guard let url = self.url else { return }
var req = URLRequest(url: url)
let params = ["id" : id,"skill" : skill]
let postString = self.getPostString(params: params)
req.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
req.httpMethod = "POST"
req.httpBody = postString.data(using: .utf8)
self.webview.load(req)
}
}
extension ContinueViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityView.stopAnimating()
}
}
在您的 WKNavigationDelegate
实施中,您可以执行如下操作:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url, url.scheme == "tel" {
UIApplication.shared.open(url)
decisionHandler(.cancel)
}else {
decisionHandler(.allow)
}
}
我在我的应用程序中使用了 webkit。
如图所示,“继续”和呼叫按钮(phone 图标)都是来自网站的操作标签。
当我点击“继续”时,它会让我进入下一页,但是当我点击呼叫按钮(phone 图标)时,没有任何反应。
我想在 ios 中单击呼叫按钮(phone 图标)时打开呼叫拨号器。
我所做的如下, 我的代码是
import UIKit
import WebKit
import Alamofire
class ContinueViewController: UIViewController, WKUIDelegate {
func getPostString(params:[String:Any]) -> String{
var data = [String]()
for(key, value) in params
{
data.append(key + "=\(value)")
}
return data.map { String([=10=]) }.joined(separator: "&")
}
@IBOutlet weak var activityView: UIActivityIndicatorView!
@IBOutlet weak var webview: WKWebView!
var url: URL?
var id = 0, skill = 0
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 14.0, *) {
webview.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
}else {
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
configuration.preferences = preferences
}
webview.navigationDelegate = self
self.navigationController?.navigationBar.isHidden = true
webview.uiDelegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
activityView.startAnimating()
guard let url = self.url else { return }
var req = URLRequest(url: url)
let params = ["id" : id,"skill" : skill]
let postString = self.getPostString(params: params)
req.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
req.httpMethod = "POST"
req.httpBody = postString.data(using: .utf8)
self.webview.load(req)
}
}
extension ContinueViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityView.stopAnimating()
}
}
在您的 WKNavigationDelegate
实施中,您可以执行如下操作:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url, url.scheme == "tel" {
UIApplication.shared.open(url)
decisionHandler(.cancel)
}else {
decisionHandler(.allow)
}
}