链接到应用名称中带有 "space" 的应用商店

Linking to app store with a "space" in app name

我想link从我的应用直接到应用商店。

我正在使用此代码:

   var url  = NSURL(string: "itms-apps://itunes.com/apps/companyname/appname")
            if UIApplication.sharedApplication().canOpenURL(url!) == true  {
                UIApplication.sharedApplication().openURL(url!)
            }

当应用程序名称中没有 space 时,它可以正常工作。但是应用程序名称中是否有一个 space,它不会 link 到应用程序本身,而是 link 到包含我所有应用程序的页面。怎么会?


示例:如果应用名称 = bobbie,我使用:

var url  = NSURL(string: "itms-apps://itunes.com/apps/mycompany/bobbie")
            if UIApplication.sharedApplication().canOpenURL(url!) == true  {
                UIApplication.sharedApplication().openURL(url!)
            }

(完美运行)

但是它 app name = bob bie ,我使用:

var url  = NSURL(string: "itms-apps://itunes.com/apps/mycompany/bob-bie")
            if UIApplication.sharedApplication().canOpenURL(url!) == true  {
                UIApplication.sharedApplication().openURL(url!)
            }

但这不会 link 应用...

为您的应用程序使用 iTunes 连接中提供的 link。

  1. 打开iTunes Connect
  2. 登录
  3. 点击"My apps"
  4. Select 你的应用程序
  5. 点击"More"
  6. 为"Show in App Store"
  7. 复制link

您只需使用 stringByAddingPercentEscapesUsingEncoding:

将 space 替换为 %20
let link = "itms-apps://itunes.com/apps/mycompany/bob bie"
if let linkWithPercentEscape = link.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
    println(linkWithPercentEscape)
    if let url  = NSURL(string: linkWithPercentEscape) {
        println(url)
        if UIApplication.sharedApplication().canOpenURL(url)  {
            UIApplication.sharedApplication().openURL(url)

        }
    }
}