Adds handling of `tel` url schemes

This commit is contained in:
Stuart Breckenridge 2020-05-03 22:37:01 +08:00
parent f901436211
commit e825a5d516
No known key found for this signature in database
GPG Key ID: 977FF9C8CE7AB207
2 changed files with 13 additions and 1 deletions

View File

@ -14,4 +14,9 @@ extension URL {
var emailAddress: String? {
scheme == "mailto" ? URLComponents(url: self, resolvingAgainstBaseURL: false)?.path : nil
}
/// Extracts telephone number from a `URL` with a `tel` scheme, otherwise `nil`.
var telNumber: String? {
scheme == "tel" ? URLComponents(url: self, resolvingAgainstBaseURL: false)?.path : nil
}
}

View File

@ -314,7 +314,7 @@ extension WebViewController: WKNavigationDelegate {
} else if components?.scheme == "mailto" {
decisionHandler(.cancel)
guard let emailAddress = components?.url?.emailAddress else {
guard let emailAddress = url.emailAddress else {
return
}
@ -328,6 +328,13 @@ extension WebViewController: WKNavigationDelegate {
alert.addAction(.init(title: "Dismiss", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else if components?.scheme == "tel" {
decisionHandler(.cancel)
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [.universalLinksOnly : false], completionHandler: nil)
}
} else {
decisionHandler(.allow)
}