How to open external urls in WebKit? - ios

I'm trying to build an external webpage in a fullscreen mobile app in iOS. Everything is working fine.
But how is it possible to open external urls (outside google.com) in Safari?
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var mWebKit: WKWebView!
let urlMy = URL(string: "https://www.google.com/")
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: urlMy!)
mWebKit.load(request)
// Do any additional setup after loading the view.
}
}
Thank you very much!

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.targetFrame == nil{
if #available(iOS 10.0, *) {
UIApplication.shared.open(navigationAction.request.url!, options: [:])
} else {
UIApplication.shared.openURL(navigationAction.request.url!)
// Fallback on earlier versions
}
}
}

Related

Handle tel:// links in WKWebView on iPad

I'm trying to handle clicks on "tel:xxxxx" links in WKWebView. The code below works on iPhone, but on an iPad the callback webView(_ webView:,decidePolicyFor:,decisionHandler:) is not called. Is there any workaround?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.loadHTMLString("<html><body>Click here to call</body></html>", baseURL: nil)
}
// MARK: - WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url {
print("navigation to url: \(url)")
}
decisionHandler(.allow)
}
}

URL is not opening in WKWebView in swift

I have a requirement to open the URL in WKWebView and login to the portal. As user logged in successfully after that I have to perform download operation from WKWebView, Everything is working fine but it's opening in external safari browser but as per requirement it should open in WKWebView
Outlet for WKWebView
#IBOutlet var webView: WKWebView!
Function to call URL in WKWebVIew
func loadURLInWebView() {
let url = URL(string: "https://www-qa.yyy.com/content/dash/en/public/login.html")
let urlRequest = URLRequest(url: url!)
if let webView = webView {
webView.load(urlRequest)
}
}
after adding decidePolicyFor delegate it's opening url in safari but it should open in WKWebView. I am not able to find the issue.
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
guard url != nil else {
print(url!)
decisionHandler(.allow)
return
}
if url!.description.lowercased().starts(with: "http://") ||
url!.description.lowercased().starts(with: "https://") {
decisionHandler(.cancel)
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
decisionHandler(.allow)
}
}
you are handling guard statement in the wrong way. In guard else, the condition will be called when the URL is nil.
If you do print(URL!) in else your app will crash. moreover when URL is nill, you should call cancel Handler.
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else { // else will be called when url is nil
decisionHandler(.cancel)
return
}
if url.description.lowercased().starts(with: "http://") ||
url.description.lowercased().starts(with: "https://") {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}

How can I tap a phone number with WKWebView

Swift 4
iOS 11.4
I'm trying to learn how to use the WKWebView but I'm having an issue where any phone-number links won't actually display the popup to call that number.
I followed this answer and the error that prints is listed below. The website I'm using is a wix site that uses the quickactionbar (I know this uses tel:xxxxx so it should be working since it works on safari) Any idea why it's doing this?
Edit:
Also, I'm not entirely sure what to search for in order to get the answer I need. If you know exactly what to search to find the answer to this, I'll be more than happy with that as well.
It recognizes it's a phone number if I long press but won't do anything if I just tap it
import Foundation
import WebKit
class WebKitViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
#IBOutlet weak var backButton: UIBarButtonItem!
#IBAction func backButtonAction(_ sender: UIBarButtonItem) {
if self.webView.canGoBack {
self.webView.goBack()
}
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "Link")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
// func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// backButton.isEnabled = webView.canGoBack
// }
//Code from answer on StackOverflow
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
print("didStartProvisionalNavigation: \(navigation)")
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation) {
print("didReceiveServerRedirectForProvisionalNavigation: \(navigation)")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation, withError error: Error) {
print("didFailProvisionalNavigation: \(navigation), error: \(error)")
}
func webView(_ webView: WKWebView, didFinishLoading navigation: WKNavigation) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
print("didFinishLoadingNavigation: \(navigation)")
}
func _webViewWebProcessDidCrash(_ webView: WKWebView) {
print("WebContent process crashed; reloading")
}
func webView(_ webView: WKWebView,createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?{
//if <a> tag does not contain attribute or has _blank then navigationAction.targetFrame will return nil
if let trgFrm = navigationAction.targetFrame {
if(!trgFrm.isMainFrame){
UIApplication.shared.isNetworkActivityIndicatorVisible = true
self.webView.load(navigationAction.request)
}
}
return nil
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
UIApplication.shared.isNetworkActivityIndicatorVisible = false
print("didFinish: \(String(describing: self.webView.url)); stillLoading:\(self.webView.isLoading ? "NO" : "YES")")
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (_: WKNavigationResponsePolicy) -> Void) {
print("decidePolicyForNavigationResponse")
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (_: WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
}
The error:
didFailProvisionalNavigation: <WKNavigation: 0x101865bd0>, error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={_WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x101856b80>, NSErrorFailingURLStringKey=tel:+12199874536, NSErrorFailingURLKey=tel:+12199874536, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x1018641a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
you can also set dataDetectorTypes to .phoneNumber on your WKWebViewConfiguration. All detected phone numbers will transformed to contain links around the phone number.
configuration.dataDetectorTypes = .phoneNumber
Use this delagate func-
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void) {
if navigationAction.request.url?.scheme == "tel" {
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}

How do you allow the WKWebView to accept redirect url

Having some trouble redirecting a user to a link from a WKWebView.
User is essentially meant to be authenticating on a website using WKWebView, the website checks the authentication which in turn redirects the user back to our app.
My initial thought is that there is some sort of restriction to allow the user to exit the app during the redirecting process. Which is why I implemented WKNavigationDelegate and saw that when we try authenticating the code below didReceive challenge runs through the second if case - which I only assume that its a good thing. Though the app does not get redirected.
Below is the code I have.
import UIKit
import WebKit
class WebViewViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isHidden = false
tabBarController?.tabBar.isHidden = true
webView.navigationDelegate = self
if let url = URL(string: "SomeUrl") {
let request = URLRequest(url: url)
webView.load(request)
}
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let URL = webView.request?.url {
OAuthHelper.oAuthManager.processOAuthStep1Response(url: URL)
}
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if challenge.previousFailureCount > 0 {
completionHandler(Foundation.URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
} else if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
} else {
print("unknown state. error: \(challenge.error)")
// do something w/ completionHandler here
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if (navigationAction.navigationType == .linkActivated){
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
}
I did not fully understand the problem. If the question is that if you want the user to redirect from Webview to Native App, then you do have different options. I will be providing the options. Let me know if this is you are looking at.
Use WKURLSchemeHandler to handle the custom URLs. Read my article about this. https://medium.com/#kumarreddy_b/custom-scheme-handling-in-uiwebview-wkwebview-bbeb2f3f6cc1. https://github.com/BKRApps/KRWebView
you will be getting the urls in func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void), here you can intercept the URL and then use your URL scheme to get into the native app. Use OpenUrl to do this. But this is deprecated.

Cannot use JavaScript to open links in mobile Safari when called from WKWebView

This post and this post addresses the problem of using JavaScript to open WKWebView links in mobile Safari, but for some reason, the solutions provided don't work.
The code below incorporates elements of different solutions, yet still the link does not open after clicking the button.
In fact, webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) and userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) don't even get called.
HTML:
<html>
<style type="text/css">
#button { width: 100px; height: 50px; background: red; }
</style>
<div id="button">CLICK TO GO</div>
<script type="text/javascript">
var button = document.getElementById("button");
button.addEventListener("click", doTest);
function doTest() {
window.open("http://www.google.com", target="_blank");
button.innerHTML = "HEY";
}
</script>
</html>
ViewController:
class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView?
let webViewContentController = WKUserContentController()
override func viewDidLoad() {
super.viewDidLoad()
let config = WKWebViewConfiguration()
config.userContentController = webViewContentController
webView = WKWebView(frame: view.bounds, configuration: config)
webView!.navigationDelegate = self
webView!.uiDelegate = self //must have this
var fileURL = URL(fileURLWithPath: Bundle.main.path(forResource:"Mooncake.html", ofType: nil)!)
webView!.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
view.addSubview(webView!)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
}
// this handles target=_blank links by opening them in the same view
func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
if navigationAction.targetFrame == nil {
let url = navigationAction.request.url!
let urlString = url.description.lowercased()
if urlString.contains("http://") || urlString.contains("https://") || urlString.contains("mailto:") {
UIApplication.shared.openURL(url)
}
}
return nil
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
}
}
The problem was using outdated function definitions, lifted from the SO answers, for
webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView!
and
webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void)
Using the Swift 3 function definitions as defined by the class docs solved the problem.

Resources