【问题标题】:Launching App Store from App in Swift在 Swift 中从 App 启动 App Store
【发布时间】:2014-11-14 09:56:04
【问题描述】:

我正在创建一个应用程序,并且我有一个横幅来宣传我的其他应用程序。这是我的代码:

var barsButton : UIButton = UIButton(frame: CGRectMake((self.view.bounds.width / 2) - 51, self.view.bounds.height - 100, 102, 30))
barsButton.setImage(UIImage(named: "Bars Icon 2.png"), forState: .Normal)
barsButton.addTarget(self, action: "openBarsLink", forControlEvents: UIControlEvents.TouchUpInside)

func openBarsLink() {
    var barsLink : String = "itms-apps:https://itunes.apple.com/app/bars/id706081574?mt=8"
    UIApplication.sharedApplication().openURL(NSURL.URLWithString(barsLink))
}

但是,当用户按下按钮时,它只会将他们带到 App Store,而不是我的应用的特定页面。我做错了什么?

【问题讨论】:

    标签: ios xcode swift app-store nsurl


    【解决方案1】:

    仅通过遵循较旧的答案我无法使其工作,所以我在这里发布我的完整解决方案:

    if let url  = NSURL(string: "itms-apps://itunes.apple.com/app/id1234567890"), 
       UIApplication.shared.canOpenURL(url) {
        
      UIApplication.shared.openURL(url)
        }
    }
    

    【讨论】:

      【解决方案2】:

      我用这个,效果很好。

      let locale: String = Locale.current.regionCode ?? "US"
      UIApplication.shared.open(URL(string: "https://apps.apple.com/\(locale)/developer/{developer-name}/{idXXXXXXXXXX}")!, options: [:], completionHandler: nil)
      

      【讨论】:

        【解决方案3】:

        您只需在实用程序结构中使用这些功能即可在应用商店中转到应用程序页面,也可以直接转到评价应用程序视图:

        static func gotoApp(appID: String, completion: ((_ success: Bool)->())? = nil) {
            let appUrl = "itms-apps://itunes.apple.com/app/id\(appID)"
        
            gotoURL(string: appUrl, completion: completion)
        }
        
        static func rateApp(appId: String, completion: ((_ success: Bool)->())? = nil) {
            //let appUrl = "itms-apps://itunes.apple.com/app/" + appId
            let appUrl = "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appId)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"
            //TODO: use &action=write-review for opening review directly
            print("app review URL: ", appUrl)
        
            gotoURL(string: appUrl, completion: completion)
        }
        
        static func gotoURL(string: String, completion: ((_ success: Bool)->())? = nil) {
            print("gotoURL: ", string)
            guard let url = URL(string: string) else {
                print("gotoURL: invalid url", string)
                completion?(false)
                return
            }
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: completion)
            } else {
                completion?(UIApplication.shared.openURL(url))
            }
        }
        

        【讨论】:

          【解决方案4】:

          由于 iOS 10 已弃用 openURL,请使用以下代码:

          UIApplication.shared.open((URL(string: "itms://itunes.apple.com/app/" + appStoreAppID)!), options:[:], completionHandler: nil)
          

          【讨论】:

            【解决方案5】:

            我遇到了这个问题,但这段代码只能在手机上运行,​​而不是在模拟器上运行。所以检查这段代码:

            if let url = URL(string: "itms-apps://itunes.apple.com/app/id" + APP_ID ),
                UIApplication.shared.canOpenURL(url){
                UIApplication.shared.openURL(url)
            }else{
                //Just check it on phone not simulator!
                print("Can not open")
            }
            

            【讨论】:

            • 对“在真实设备上打开它”的好呼声,我以为我快疯了。
            【解决方案6】:

            只使用简短的“itms://”。

            对于 Swift 3,这是 sn-p:

            UIApplication.shared.openURL(URL(string: "itms://itunes.apple.com/app/id" + appStoreAppID)!)
            

            我希望这对某人有所帮助。

            干杯。

            附: @Eric Aya 领先于时代 :)

            【讨论】:

            • @Eric D,谢谢。但是我使用的是 XCode 8,beta 3,它只适用于“shared()”
            • 啊,对不起,我的错。我很确定这个也改变了。我猜他们还没有完成对 API 的修改。 :)
            【解决方案7】:

            Swift 3 - XCode 8.2.1

            UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!)
            

            【讨论】:

              【解决方案8】:

              您尝试打开的链接无效 - 从中​​删除 https: 架构(或 itms: - 但我建议第一个选项,以避免重定向)

              【讨论】:

                【解决方案9】:

                您的网址中有太多协议。去掉 https: 以便 URL 读取

                itms-apps://itunes.apple.com/app/bars/id706081574

                【讨论】:

                  猜你喜欢
                  • 2023-04-08
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-03-01
                  • 1970-01-01
                  • 2017-05-24
                  相关资源
                  最近更新 更多