Itunes app links are not working with WkWebview - ios

One of the webpage I load into Wkwebview has the following iTunes app link
https://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8
when it's opened I'm getting the following alert
and here's the error that I've got.
{
"[errorCode]" = 0;
"[errorDescription]" = "Redirection to URL with a scheme that is not HTTP(S)";
"[errordetail]" = "Con:myappxxxx:myorder:webview:networkerror";
"[localizedRecoverySuggestion]" = "";
"[url]" = "itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263";
}
When the same iTunes link ( https://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8 )
is opened in UIWebview , I saw that URL gets redirected to following URL and app opens in appstore
itms-appss://itunes.apple.com/gb/app/xx-yy-zz/id435919263?mt=8
Whereas in Wkwebview , the URL gets redirected to following URL
itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263
Any help is appreciated
Update
I even tried Arbitrary uploads to true for transport security and the problem is still there.
Error Domain= Code=0 "Redirection to URL with a scheme that is not
HTTP(S)"
UserInfo={_WKRecoveryAttempterErrorKey=,
NSErrorFailingURLStringKey=itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263,
NSErrorFailingURLKey=itms-appss://apps.apple.com/gb/app/xx-yy-zz/id435919263,
NSLocalizedDescription=Redirection to URL with a scheme that is not
HTTP(S)}

I think you could try to intercept the itunes link in wkwebview's delegate methods and open the URL using openURL
The below source code will open any itms-appss links in wkwebview. Don't forget to conform to WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if ([webURL.scheme isEqualToString:#"itms-appss"])
{
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:webURL])
{
[self.webviewObj stopLoading];
[app openURL:[NSURL URLWithString:[webURL absoluteString]]];
decisionHandler(WKNavigationActionPolicyCancel);
} else{
decisionHandler(WKNavigationActionPolicyCancel);
}
}
else
{
decisionHandler(WKNavigationActionPolicyAllow);
}
return;
}

WKWebView seems not to handle non-http(s) url schemas by default.
So, you have to catch the request using webView(_:decidePolicyFor:decisionHandler:), and check the url that can be loaded by WKWebView.
If the url is non-http(s) url, then you should open the url by open(_:options:completionHandler:).
Here the sample code.
Assign the navigationDelegate for your WKWebViewinstance.
webView.navigationDelegate = self
Implement WKNavigationDelegate method.
func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
// if the url is not http(s) schema, then the UIApplication open the url
if let url = navigationAction.request.url,
!url.absoluteString.hasPrefix("http://"),
!url.absoluteString.hasPrefix("https://"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
// cancel the request
decisionHandler(.cancel)
} else {
// allow the request
decisionHandler(.allow)
}
}

Try in your info.plist adding:
App transport Security settings
-- Allow Arbitrary loads = yes
<key>NSAppTransportSecurity</key>
<dict>
<!--Connect to anything (this is probably BAD)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Related

How to redirect from a button click inside WKWebview and to go back to the iOS application using objective c?

I am developing a native iOS application . On a button click I am loading a website app inside the WKWebview. Inside the web view after logging in with username and password the following permission screen comes to us which has "Allow" and "Deny" button.
What I want is when pressing on the "Allow" button the current screen should get dismissed and redirect back to my application. Similarly with the "Deny" button . After so much of research I found the following Swift code for the WKWebviewDelegate:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
// here we handle internally the callback url and call method that call handleOpenURL (not app scheme used)
if let url = navigationAction.request.url, url.host == "localhost" , url.path == "/square-oauth-callback" {
print(url)
print(url.valueOf("code"))
//decisionHandler(.cancel)
/* Dismiss your view controller as normal
And proceed with OAuth authorization code
The code you receive here is not the auth token; For auth token you have to make another api call with the code that you received here and you can procced further
*/
/*
Auth Process Flow: https://developer.squareup.com/docs/oauth-api/how-it-works#oauth-access-token-management
Obtain Auth Token: https://developer.squareup.com/reference/square/oauth-api/obtain-token
*/
}
decisionHandler(.allow)
}
Am I correct in my approach?
If yes can someone please tell the Objective-C alternative of the above code? Sorry for the case as I recently started my career using Swift and I have very little knowledge about Objective-C. If no, can someone suggest me the correct approach?
The same delegate method can be used in objective-C also:
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
Objective-C conversion of above code:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if ([navigationAction.request.URL isEqual:#"localhost"] && [navigationAction.request.URL.path isEqual:#"/square-oauth-callback"]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSLog(#"URL:%#",navigationAction.request.URL);
}
else
{
decisionHandler(WKNavigationActionPolicyAllow);
}
}
Apple Documentation: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview?language=objc

Opening external links in WKWebView (not externally)

My app contains HTML information pages opened in WKWebView, containing some hyperlinks. These used to work, but stopped working with recent iOS releases with error messages, containing words
Could not create sandbox extension
Connection to daemon was invalidated
When I apply WKNavigation delegate with the following code
extension InformationPageController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
var policy = WKNavigationActionPolicy.allow
if navigationAction.navigationType == .linkActivated {
// Neither of commented lines below works!
// webView.load(navigationAction.request)
// webView.loadHTMLString(navigationAction.request.url!.absoluteString, baseURL: nil)
UIApplication.shared.openURL(navigationAction.request.url!)
policy = .cancel
}
decisionHandler(policy)
}
}
the pages are opened in an external browser, but not inside the app.
Is there a way to open the links as before, within same WKWebView?
It looks like only https protocol is currently accepted by WKWebView. If you have just http, WKWebView will complain about insecure protocol and won't open.
After I changed http-s to https-s, the pages opened, but not AppStore pages. This leads to another issue.
When URL address starts with https://apps.apple.com/..., iOS (but not macOS) redirects it to itms-appss://apps.apple.com/... for opening in AppStore app, causing WKWebView to come up with the error: Redirection to URL with a scheme that is not HTTP(S). Similar problem with mailto urls.
To bypass these issues I modified code to the following:
extension InformationPageController: WKNavigationDelegate {
static let disallowedUrlPrefixes = ["https://apps.apple.com", "mailto:"]
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
var policy = WKNavigationActionPolicy.allow
if navigationAction.navigationType == .linkActivated,
let url = navigationAction.request.url {
let urlString = url.absoluteString
if InformationPageController.disallowedUrlPrefixes.first(where:{urlString.starts(with: $0)}) != nil {
UIApplication.shared.openURL(url)
policy = .cancel
}
}
decisionHandler(policy)
}
}
Now the 'troublesome' pages are opened with an external browser, all others are opened within WKWebView. Unfortunately that's the best I can currently think of.
Perhaps it's because an iOS app by default doesn't allow http requests. Try to allow it by adding "App Transport Security Settings" and add a sub key "Allow Arbitrary Loads" and set it to true in the app's info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

IOS - WKWebview gets null url in decidePolicy when second clicking the same button

I have a WKWebview with a button that downloads a pdf, the button link is as follows:
<img class= src="$imageurl">
I have managed to download the PDF through certain logic, that has a starting point in decidePolicyFor method:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
if openInDocumentPreview(url!) {
decisionHandler(.cancel)
executeDocumentDownloadScript(webView, forAbsoluteUrl: url!.absoluteString)
} else {
decisionHandler(.allow)
}
}
This works well, and the url is the one with the PDF. However, if I click the button again I only receive null urls, so I cannot open the document again. This is the action I receive:
<WKNavigationAction: 0x102d95fa0; navigationType = -1; syntheticClickType = 0; position x = 0.00 y = 0.00 request = <NSMutableURLRequest: 0x282e151d0> { URL: about:blank }; sourceFrame = <WKFrameInfo: 0x102d502f0; webView = 0x103048200; isMainFrame = YES; request = <NSMutableURLRequest: 0x282e116d0> { URL: https://mysite.appiancloud.com/suite/sites/home }>; targetFrame = <WKFrameInfo: 0x102d577a0; webView = 0x103048200; isMainFrame = NO; request = (null)>>
If I test this on Safari Desktop the file is downloaded ok every time, however I don't find the reason why WKWebView stops receiving the url. No other delegate method is called, not even the createWebViewWith which tends to be called when target="_blank" urls are invoked.
I wonder if WKWebview cachePolicy is taking effect, but I don't find the way to avoid that url caching, or if it is being cached, to receive the event on when it is trying to load it again.
Also, as a note, if I long-click the link, the preview element holds the URL correctly.
Ok, my theory is that by telling the handler that WKNavigationAction policy is .cancel, following requests won't be processed.
What I did is to process the URL download in the createWebViewWith method, and let the policy to be always .allow.

Why is WKWebView not opening a telephone link in ios 9?

I have a WKWebView that loads a webpage with some telephone links on the webpage.
Currently i have this code to handle clicks to those links.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.request.url?.scheme == "tel" {
if #available(iOS 10.0, *) {
UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(navigationAction.request.url!)
}
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
This works fine on any device with ios 10 installed, i am prompted with an alert box asking to either cancel or make a call. but on ios 9 devices the telephone app screen flashes (no prompt) and it nothing happens after.
Key concepts before proceed :
By default, a web view automatically converts telephone numbers that appear in web content to Phone links. When a Phone link is tapped, the Phone app launches and dials the number. The tel URL scheme is used to launch the Phone app on iOS devices and initiate dialing of the specified phone number.
from the html side the web page showing phone numbers must be like this :
<body>
<!-- Then use phone links to explicitly create a link. -->
<p>A phone number: 1-408-555-5555</p>
<!-- Otherwise, numbers that look like phone numbers are not links. -->
<p>Not a phone number: 408-555-5555</p>
</body>
After this short preamble, theres two behaviour for WKWebView , one for iOS 9 and other for iOS 10 and later.
on iOS 10 Apple introduced a property called dataDetectorTypes on WKWebViewConfiguration and by default this property have the value WKDataDetectorTypeNone wich means that does not detect nothing. So in the case you want to detect phone numbers and links (just an example) the configuration for WKWebView would look like this (suppose you have a variable _webView of type WKWebView):
WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
if ([configuration respondsToSelector:#selector(dataDetectorTypes)]) {
configuration.dataDetectorTypes = UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
}
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
with this configuration your webView will be able to handle everything under the hood.
Now on 9 > iOS < 10 (iOS between 9 and 10) your delegate decision handler would look like this :
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if ([navigationAction.request.URL.scheme isEqualToString:#"tel"]){
[UIApplication.sharedApplication openURL:navigationAction.request.URL];
decisionHandler(WKNavigationActionPolicyCancel);
}else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
and this will open the phone app. Keep in mind that:
iOS 10.3 and later displays an alert and requires user confirmation
before dialing. (When this scenario occurs in versions of iOS prior to
10.3, iOS initiates dialing without further prompting the user and does not display an alert, although a native app can be configured to
display its own alert.
Please do not follow any instruction talking about URL scheme whitelist , this is another story that apple introduce on iOS 9, but this is when you need to declare url scheme handlers, for now the tel scheme is handled by iOS.
Hope this help.
Swift 4:
webView.navigationDelegate = self
webView.configuration.dataDetectorTypes = [.link, .phoneNumber]
extension PDFWebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if let requestUrl = navigationAction.request.url, requestUrl.scheme == "tel" {
UIApplication.shared.open(requestUrl, options: [:], completionHandler: nil)
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
}

Unsupported URL error clicking a Whatsapp scheme with an URL in a iOS WKWebView

I'm using iOS 9.3.3 and clicking a Whatsapp link in a website displayed in a WKWebView.
Whenever I try to send a message to the whatsapp url scheme with a URL that includes the http:// or https:// part of the link as part of the message I get an "Unsupported URL" error.
[self.webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"whatsapp://send?text=this%20is%20a%20test%20http://https://www.usa-brands.net/collections/new-arrivals/products/dacey-cap-sleeve-drop-waist-wool-sweater-dress"]]];
When I take out the http:// or https:// it works but now I don't get a Rich Preview (https://www.macstories.net/ios/whatsapp-adds-rich-previews-for-web-links/).
I've tried encoding the url but this doesn't work either. When I remove all other potential problems it comes down to the protocol in the url link.
The full error message is:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={_WKRecoveryAttempterErrorKey=, NSErrorFailingURLStringKey=whatsapp://send?text=Check%20this%20out%20'Dacey'%20Cap%20Sleeve%20Drop%20Waist%20Wool%20Sweater%20Dress,%20348.00%20USD:%20https://www.usa-brands.net/products/dacey-cap-sleeve-drop-waist-wool-sweater-dress, NSErrorFailingURLKey=whatsapp://send?text=Check%20this%20out%20'Dacey'%20Cap%20Sleeve%20Drop%20Waist%20Wool%20Sweater%20Dress,%20348.00%20USD:%20https://www.usa-brands.net/products/dacey-cap-sleeve-drop-waist-wool-sweater-dress, NSUnderlyingError=0x137dd1380 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "unsupported URL" UserInfo={NSErrorFailingURLStringKey=whatsapp://send?text=Check%20this%20out%20'Dacey'%20Cap%20Sleeve%20Drop%20Waist%20Wool%20Sweater%20Dress,%20348.00%20USD:%20https://www.usa-brands.net/products/dacey-cap-sleeve-drop-waist-wool-sweater-dress, NSLocalizedDescription=unsupported URL, NSErrorFailingURLKey=whatsapp://send?text=Check%20this%20out%20'Dacey'%20Cap%20Sleeve%20Drop%20Waist%20Wool%20Sweater%20Dress,%20348.00%20USD:%20https://www.usa-brands.net/products/dacey-cap-sleeve-drop-waist-wool-sweater-dress}}, NSLocalizedDescription=unsupported URL}
Does anyone have any idea why this doesn't work. It works for Android.
You should implement the WKWebView's WKNavigationDelegate function:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (![url.scheme isEqualToString:#"http"] && ![url.scheme isEqualToString:#"https"]) {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else {
NSLog(#"Sorry, you haven't install the %#", url.scheme);
}
decisionHandler(NO);
}
decisionHandler(YES);
}
And don't forget add whatsapp's white list at Info.plist:
<dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
......
</dict>
Below method:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void)
Will be invoked multiple times (depending on the website) and inside this method you will need to check the url request of the navigationItem, in other words, we are giving the opportunity to intercept a request before it is being send and decide if we can continue with it or not:
if let requestURL = navigationAction.request.url?.absoluteString,
!requestURL.contains("https") && !requestURL.contains("http") {
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
If the request contains https or http it means we can safely navigate wherever we want. If not, you will have to add multiple validation like in #广锅锅 answer for example.

Resources