在Swift中从App启动App Store
我正在创build一个应用程序,我有一个横幅,促进我的其他应用程序。 这是我的代码:
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)) }
但是,当用户按下button时,它只是把他们带到App Store,而不是我的应用程序的特定页面。 我究竟做错了什么?
您的url中有太多的协议。 摆脱https:
所以URL读取
itms-apps://itunes.apple.com/app/bars/id706081574
只要按照以前的答案,我不能让它工作,所以在这里我发布我的完整解决scheme:
var url = NSURL(string: "itms-apps://itunes.apple.com/app/bars/id706081574") if UIApplication.sharedApplication().canOpenURL(url!) { UIApplication.sharedApplication().openURL(url!) }
只使用简短的“itms://”。
对于Swift 3来说,这是片段:
UIApplication.shared.openURL(URL(string: "itms://itunes.apple.com/app/id" + appStoreAppID)!)
我希望这可以帮助别人。
干杯。
PS @Eric绫提前:)
Swift 3 – XCode 8.2.1
UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!)
我有这个问题,但是这个代码只能在手机而不是模拟器上工作。 所以检查这个代码:
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") }
你试图打开的链接是无效的 – 删除https:架构从它(或itms: – 但我build议第一个选项,以避免redirect)
由于openURL已从iOS 10中弃用,请使用以下代码:
UIApplication.shared.open((URL(string: "itms://itunes.apple.com/app/" + appStoreAppID)!), options:[:], completionHandler: nil)