【问题标题】:How can I understand if UIApplication is going to open link in Safari app?我如何了解 UIApplication 是否要在 Safari 应用程序中打开链接?
【发布时间】:2015-03-15 03:17:01
【问题描述】:

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

- (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 应用程序中打开链接?

【问题讨论】:

  • 看方案。 httphttps URL 将在 Safari 中打开。 mailto打开邮件应用等
  • @rmaddy 但应用​​商店链接具有 http 方案,应在应用商店应用程序中打开
  • App Store 链接以itms:开头。
  • @maddy,是的,你是对的

标签: ios iphone safari mobile-safari wkwebview


【解决方案1】:

嗯,基本上,你无法区分这一点,因为 App Store 链接可能是 https:// 链接。

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

Apple 建议您只使用openURL:,就像您在上面所做的那样。见https://developer.apple.com/library/ios/qa/qa1629/_index.html

如果你真的想要区分它们并做一些更花哨的事情(例如像这样使用 StoreKit:Possible to show a "in App Store modal" in iOS 6?),你别无选择,只能使用正则表达式解析每个链接像这样:

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 等。

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2023-03-26
    • 2014-04-23
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    相关资源
    最近更新 更多