WKWebView:html 内容中的 mailto 链接未打开邮件应用程序
WKWebView: mailto links in html content not opening mail app
我创建了一个非常简单的 iOS 应用程序 (Swift 5)。它只是一个 WKWebView
加载我的 PWA url.
除所有 <a href="mailto:name@name.com">Mail me</a>
链接外,一切正常。当我点击它们时,没有任何反应,我的邮件应用程序没有打开。
这是我的代码 ViewController.swift
:
//
// ViewController.swift
// panel
//
// Created by kevin on 25/07/2019.
// Copyright © 2019 umono. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://someUrlToMyApp.appspot.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
if #available(iOS 11.0, *) {
webView.scrollView.contentInsetAdjustmentBehavior = .never;
}
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = [.all]
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
编辑:
谢谢,这是我的工作代码:
//
// ViewController.swift
// panel
//
// Created by kevin on 25/07/2019.
// Copyright © 2019 umono. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://someUrlToMyApp.appspot.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
if #available(iOS 11.0, *) {
webView.scrollView.contentInsetAdjustmentBehavior = .never;
}
webView.navigationDelegate = self
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = [.all]
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard
let url = navigationAction.request.url else {
decisionHandler(.cancel)
return
}
let string = url.absoluteString
if (string.contains("mailto:")) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}
做你想做的事情的一种方法是实现 WKNavigationDelegate:
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard
let file = Bundle.main.path(forResource: "test", ofType: "html"),
let html = try? String(contentsOfFile: file) else {
return
}
webView.navigationDelegate = self
webView.loadHTMLString(html, baseURL: nil)
}
@IBAction func didTapButton(_ sender: Any) {
let email = "email@email.com"
guard
let url = URL(string: "mailto:\(email)") else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard
let url = navigationAction.request.url,
let scheme = url.scheme else {
decisionHandler(.cancel)
return
}
if (scheme.lowercased() == "mailto") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
// here I decide to .cancel, do as you wish
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}
这里有一个 ViewController,它有 webView
作为出口,这个 WKWebView 会像这样加载一个 html 文件:
<a href="mailto:email@email.com">Mail me</a>
我还在情节提要中添加了一个按钮,仅供参考,该按钮具有上述 IBAction didTapButton
。
这里的关键是:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
哪个会给你 URL 让你决定什么政策适合它。在这里我检查它是否包含 mailto:
因为我已经知道这是你感兴趣的所以如果它包含,我只需打开 URL 就像用户按下 [=16= 时我会做的那样] 在屏幕上可见。
希望对你有帮助,干杯!
LE:确保你运行在真实设备上(模拟器没有安装邮件应用程序),还要确保你安装了邮件应用程序,因为我没有..
我创建了一个非常简单的 iOS 应用程序 (Swift 5)。它只是一个 WKWebView
加载我的 PWA url.
除所有 <a href="mailto:name@name.com">Mail me</a>
链接外,一切正常。当我点击它们时,没有任何反应,我的邮件应用程序没有打开。
这是我的代码 ViewController.swift
:
//
// ViewController.swift
// panel
//
// Created by kevin on 25/07/2019.
// Copyright © 2019 umono. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://someUrlToMyApp.appspot.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
if #available(iOS 11.0, *) {
webView.scrollView.contentInsetAdjustmentBehavior = .never;
}
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = [.all]
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
编辑:
谢谢,这是我的工作代码:
//
// ViewController.swift
// panel
//
// Created by kevin on 25/07/2019.
// Copyright © 2019 umono. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://someUrlToMyApp.appspot.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
if #available(iOS 11.0, *) {
webView.scrollView.contentInsetAdjustmentBehavior = .never;
}
webView.navigationDelegate = self
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = [.all]
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard
let url = navigationAction.request.url else {
decisionHandler(.cancel)
return
}
let string = url.absoluteString
if (string.contains("mailto:")) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}
做你想做的事情的一种方法是实现 WKNavigationDelegate:
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard
let file = Bundle.main.path(forResource: "test", ofType: "html"),
let html = try? String(contentsOfFile: file) else {
return
}
webView.navigationDelegate = self
webView.loadHTMLString(html, baseURL: nil)
}
@IBAction func didTapButton(_ sender: Any) {
let email = "email@email.com"
guard
let url = URL(string: "mailto:\(email)") else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard
let url = navigationAction.request.url,
let scheme = url.scheme else {
decisionHandler(.cancel)
return
}
if (scheme.lowercased() == "mailto") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
// here I decide to .cancel, do as you wish
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}
这里有一个 ViewController,它有 webView
作为出口,这个 WKWebView 会像这样加载一个 html 文件:
<a href="mailto:email@email.com">Mail me</a>
我还在情节提要中添加了一个按钮,仅供参考,该按钮具有上述 IBAction didTapButton
。
这里的关键是:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
哪个会给你 URL 让你决定什么政策适合它。在这里我检查它是否包含 mailto:
因为我已经知道这是你感兴趣的所以如果它包含,我只需打开 URL 就像用户按下 [=16= 时我会做的那样] 在屏幕上可见。
希望对你有帮助,干杯!
LE:确保你运行在真实设备上(模拟器没有安装邮件应用程序),还要确保你安装了邮件应用程序,因为我没有..