我如何才能知道UIApplication是否要在Safari应用程序中打开链接? [英] How can I understand if UIApplication is going to open link in Safari app?

查看:109
本文介绍了我如何才能知道UIApplication是否要在Safari应用程序中打开链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发浏览器.我需要在同一WKWebView实例中打开新的浏览器窗口,但是我还需要能够在Appstore应用程序中打开链接.

I'm developing a browser. I need to open new browser windows in the same WKWebView instance, but also I need to be able opening links in 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;
}

问题是它会在Safari应用程序中打开所有新窗口. 我如何才能知道UIApplication是否要在Safari应用程序中打开链接?

the problem is that it opens all new windows in Safari app. How can I understand if UIApplication is going to open link in Safari app?

推荐答案

好吧,基本上,您无法区分这一点,因为App Store链接可能是https://链接.

Well, basically, you can´t distinguish this, as App Store links could be https:// links.

示例链接: https://itunes.apple.com/de/app/myLeetApp/id313370815?mt = 8

Apple建议您像上面一样使用openURL:.参见 https://developer.apple.com/library/ios/qa /qa1629/_index.html

Apple suggests to just use openURL: as you´re doing above. See https://developer.apple.com/library/ios/qa/qa1629/_index.html

如果您真的真的要区分它们并做一些更有趣的事情(例如,使用StoreKit这样:),您别无选择像这样用正则表达式解析每个链接:

If you really really want to distinguish them and do something more fancy (e.g. using StoreKit like this: Possible to show a "in App Store modal" in iOS 6?), you´ve got no other option than to parse every link with a regex like 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")
    }
}

使用extractedAppID,然后可以打开SKStoreProductViewController等.

With the extractedAppID you then can open the SKStoreProductViewController etc.

这篇关于我如何才能知道UIApplication是否要在Safari应用程序中打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆