How can I understand if UIApplication will open a link in a Safari application?

I am developing a browser. I need to open new browser windows in one instance of WKWebView, but also I need to open links in the Appstore application.

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { if (navigationAction.targetFrame == nil) { NSURL *url = navigationAction.request.URL; UIApplication *app = [UIApplication sharedApplication]; if ([app canOpenURL:url]) { [app openURL:url]; } } decisionHandler(WKNavigationActionPolicyAllow); } #pragma mark - WKUIDelegate - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { if (!navigationAction.targetFrame.isMainFrame) { [webView loadRequest:navigationAction.request]; } return nil; } 

the problem is that it opens all new windows in the Safari application. How can I understand if UIApplication will open a link in a Safari application?

0
safari ios iphone mobile-safari wkwebview
Mar 15 '15 at 3:17
source share
1 answer

In general, you cannot distinguish this, since links in the App Store can be https: // links.

Link example: https://itunes.apple.com/de/app/myLeetApp/id313370815?mt=8

Apple suggests just using openURL: as you do above. See https://developer.apple.com/library/ios/qa/qa1629/_index.html

If you really want to distinguish them and do something more bizarre (for example, using StoreKit as follows: Is it possible to show "in the App Store modal store" in iOS 6? ), You have no choice but to parse each link with a regular expression So:

 import UIKit let url = "https://itunes.apple.com/de/app/myLeetApp/id313370815?mt=8" if url.rangeOfString("itunes.apple.com") != nil { let pattern = "^https?:\\/\\/itunes\\.apple\\.com\\/.*id([0-9]*).*$" if let regex = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: nil) { let extractedAppID = regex.stringByReplacingMatchesInString(url, options: nil, range: NSMakeRange(0, countElements(url)), withTemplate: "$1") } } 

With extractedAppID you can open SKStoreProductViewController , etc.

+1
Mar 15 '15 at 10:23
source share



All Articles