在Swift中从App启动App Store

use*_*282 18 xcode nsurl app-store ios swift

我正在创建一个应用程序,我有一个促销我的其他应用程序的横幅.这是我的代码:

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))
}
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*kas 23

您的URL中有太多协议.摆脱https:URL读取

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


And*_*rej 15

只是按照旧的答案我无法使它工作,所以在这里我发布我的完整解决方案:

var url  = NSURL(string: "itms-apps://itunes.apple.com/app/bars/id706081574")
if UIApplication.sharedApplication().canOpenURL(url!) {
    UIApplication.sharedApplication().openURL(url!)
}
Run Code Online (Sandbox Code Playgroud)


Sas*_*sho 6

只使用简短的"itms://".

对于Swift 3,这是片段:

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

我希望这可以帮助别人.

干杯.

PS @Eric Aya领先于时间:)