打开 url 在 Swift 中失败

Opening a url is failing in Swift

回调中的断点和记录成功显示 false。

我将 itms-apps 添加到我的 plist:LSApplicationQueriesSchemes

并将其放入我的应用委托中:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return true
}

func moreTapped() {
    let url = NSURL(string: "itms-apps://itunes.com/developer/quantum-productions/id979315877")
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url! as URL, options: [:], completionHandler: {(success: Bool) in

        })
    } else {
        UIApplication.shared.openURL(url! as URL)
    }
}

您的 URL 格式不正确,您应该在 Swift 中使用 URL 而不是 NSURL

这将在 App Store 应用程序中打开您的开发者页面:

if let url = URL(string:"itms-apps://geo.itunes.apple.com/developer/quantum-productions/id979315877&mt=8") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

对于 http/sitms 等标准 URL 类型,无需调用 canOpenURL

Apple 刚刚公布了 appstore.com 个 URL。

**

https://developer.apple.com/library/ios/qa/qa1633/_index.html

**

To create an App Store Short Link, apply the following rules to your company or app name:

Remove all whitespace

Convert all characters to lower-case

Remove all copyright (©), trademark (™) and registered mark (®) symbols

Replace ampersands ("&") with "and"

Remove most punctuation (See Listing 2 for the set)

Replace accented and other "decorated" characters (ü, å, etc.) with their elemental character (u, a, etc.)

Leave all other characters as-is.

Listing 2 Punctuation characters that must be removed.

!¡"#$%'()*+,-./:;<=>¿?@[]^_`{|}~

Below are some examples to demonstrate the conversion that takes place.

App Name examples

Ocarina => http://appstore.com/ocarina

根据 Apple 的最新文档 items-apps: 或 items: 将无法正常工作。你需要使用

appStoreLink = "https://itunes.apple.com/us/app/apple-store/id375380948?mt=8"

or

> SKStoreProductViewController