Reputation: 2370
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.
Upvotes: 2
Views: 3451
Reputation: 4803
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.
Upvotes: 0
Reputation: 31
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>
Upvotes: 3