xcode 在 Swift 中从 App 启动 App Store

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25940109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:37:33  来源:igfitidea点击:

Launching App Store from App in Swift

iosxcodeswiftapp-storensurl

提问by user2397282

I am creating an app, and I have a banner which promotes my other app. This is my code:

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

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

However, when the user presses the button, it just takes them to the App Store, and not the specific page for my app. What am I doing wrong?

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

回答by Chris Droukas

You have too many protocols in your URL. Get rid of https:so the URL reads

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

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

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

回答by Andrej

Just by following older answers I couldn't make it work, so here I post my complete solution:

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

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

回答by Sasho

Use just the short "itms://".

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

For Swift 3this is the snippet:

对于Swift 3,这是片段:

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

I hope this helps someone.

我希望这可以帮助别人。

Cheers.

干杯。

P.S. @Eric Aya was ahead of the time :)

PS @Eric Aya 领先于时间:)

回答by reza_khalafi

I had this problem but this code just works on the phone not simulator. So check this code:

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

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

回答by Amr Lotfy

Simply you can use these functions in a utility struct to goto apppage in app store also you can goto rate appview directly:

简单地,您可以在实用程序结构中使用这些函数来转到应用商店中的应用页面,也可以直接转到评价应用视图:

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

回答by ergunkocak

Swift 3 - XCode 8.2.1

Swift 3 - XCode 8.2.1

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

回答by iVarun

As openURL is deprecated from iOS 10 use below code:

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

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

回答by mlepicki

Link you are trying to open is not valid - remove https: schema from it (or itms: - but I suggest first option, to avoid redirects)

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