在Swift中从App启动App Store

时间:2014-09-19 18:19:43

标签: ios xcode swift app-store nsurl

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

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,而不是我的应用程序的特定页面。我做错了什么?

9 个答案:

答案 0 :(得分:22)

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

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

答案 1 :(得分:15)

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

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

答案 2 :(得分:6)

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

对于 Swift 3 ,这是片段:

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

我希望这有助于某人。

干杯。

P.S。 @Eric Aya领先于时间:)

答案 3 :(得分:3)

Swift 3 - XCode 8.2.1

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

答案 4 :(得分:3)

我有这个问题,但这段代码只适用于手机而不是模拟器。所以检查一下这段代码:

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")
}

答案 5 :(得分:2)

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

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))
    }
}

答案 6 :(得分:1)

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

答案 7 :(得分:1)

由于iOS 10不推荐使用openURL,请使用以下代码:

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

答案 8 :(得分:0)

我使用它并且它有效。

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