通过按钮打开AppStore

80

你们能帮我把以下代码翻译成Swift吗?

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4"]];

(还是我必须使用此链接:itms://itunes.apple.com/app/id839686104?)


4
问题寻求作业帮助的必须包括你已经完成的工作摘要以解决问题,并描述你在解决问题时遇到的困难。我知道这不是作业问题,但你仍然需要展示一些尝试过的努力。 - Bryan Chen
16个回答

156

这里。但我强烈建议你学习Swift的基础知识!

UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")!)

如果你想在Swift 5中打开AppStore:

if let url = URL(string: "itms-apps://itunes.apple.com/app/id1629135515") {
    UIApplication.shared.open(url)
}

这是否有效地允许用户将应用程序“赠送”给第三方?x-gift参考? - Katherine Jenkins
1
哈哈,X-Gift 是我应用的名称 @KatherineJenkins - LinusGeffarth
需要使用"id"吗?还是只能使用带数字的aped? - Johnykutty
@Adam 是的,这是通过关联域和深度链接实现的。假设你正在开发的应用程序响应自定义方案 mycoolapp://。这意味着如果其他应用程序在具有“mycoolapp://”方案的URL上调用openUrl,并且您的应用程序存在于该设备上,则该设备将尝试使用您的应用程序打开该链接。链接的处理方式由您的应用程序确定。 - FateNuller
7
请注意,这将打开 iTunes Store 应用程序而不是 App Store(因此应使用 itms-apps://)。 - gabuchan
显示剩余2条评论

75

Swift 3语法并使用'if let'进行改进

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

更新于7/5/17(感谢Oscar指出):

if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
    UIApplication.shared.canOpenURL(url)
{
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}

5
不错,但是openURL()已经被弃用了。现在我们应该使用这个来实现相同的行为 UIApplication.shared.open(url, options: [:], completionHandler: nil) - Oscar
1
现在您还可以将 {action=write-review} 添加到 URL 字符串中,以直接跳转到评分和撰写评论页面... - Ahmed Khedr

20

我使用这个组合,它更适合评级/购物。

(部分内容来自此处

    @IBAction func rateMe(sender: AnyObject) {
    if #available(iOS 8.0, *) {
        openStoreProductWithiTunesItemIdentifier("107698237252");
    } else {
        var url  = NSURL(string: "itms://itunes.apple.com/us/app/xxxxxxxxxxx/id107698237252?ls=1&mt=8")
        if UIApplication.sharedApplication().canOpenURL(url!) == true  {
            UIApplication.sharedApplication().openURL(url!)
        }

    }
}
func openStoreProductWithiTunesItemIdentifier(identifier: String) {
    let storeViewController = SKStoreProductViewController()
    storeViewController.delegate = self

    let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
    storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
        if loaded {
            // Parent class of self is UIViewContorller
            self?.presentViewController(storeViewController, animated: true, completion: nil)
        }
    }
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
    viewController.dismissViewControllerAnimated(true, completion: nil)
}
不要忘记导入和委托:

不要忘记导入和委托:

import StoreKit

class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {

为StoreKit加1。这是比仅仅打开AppStore更好的方式。 - LinusGeffarth
使用StoreKit的最佳答案。 - Eman

15

使用完成处理程序的Swift 4:

请确保在appStoreUrlPath中更新您的id

func openAppStore() {
    if let url = URL(string: "itms-apps://itunes.apple.com/app/id..."),
        UIApplication.shared.canOpenURL(url){
        UIApplication.shared.open(url, options: [:]) { (opened) in
            if(opened){
                print("App Store Opened")
            }
        }
    } else {
        print("Can't Open URL on Simulator")
    }
}

self.track() 是做什么用的? - Dmytro Rostopira
在这种情况下,我正在调用我的分析提供商以添加一条记录,即用户打开了应用商店。但是我刚刚删除了这行,因为它与此无关。谢谢你的问题! - mobilecat

12

用于Swift 5(已测试代码)打开App Store链接

if let url = URL(string: "https://itunes.apple.com/in/app/your-appName/id123456?mt=8") 
{
           if #available(iOS 10.0, *) {
              UIApplication.shared.open(url, options: [:], completionHandler: nil)
           }
           else {
                 if UIApplication.shared.canOpenURL(url as URL) {
                    UIApplication.shared.openURL(url as URL)
                }
           }
} 

11

其他答案对我没有用(Swift,Xcode 6.1.1),所以在这里发布我的解决方案:

var url  = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")

if UIApplication.sharedApplication().canOpenURL(url!) {
    UIApplication.sharedApplication().openURL(url!)
}

7

Swift 5.0:

导入 StoreKit 模块

extension YourViewController: SKStoreProductViewControllerDelegate {
    func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
        let storeViewController = SKStoreProductViewController()
        storeViewController.delegate = self

        let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
        storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
            if loaded {
                self?.present(storeViewController, animated: true, completion: nil)
            }
        }
    }
    private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
        viewController.dismiss(animated: true, completion: nil)
    }
}

// How to use

openStoreProductWithiTunesItemIdentifier("12345")

什么是标识符字符串? - Yogesh Patel
@YogeshPatel 这是应用程序网址中的应用程序ID,例如下面是网址,则应用程序ID将为123456789 https://apps.apple.com/us/app/id123456789 - user1039695

4
在Swift 3中检查iTunes上是否有更新的应用程序。
let currentAppVersion = Bundle.main.infoDictionary
Alamofire.request("http://itunes.apple.com/jp/lookup/?id=548615", method: .get, parameters: nil, headers: nil).responseJSON { response in
        if let value = response.result.value as? [String: AnyObject] {
            let versionNum = value["results"]?.value(forKey: "version") as? NSArray
            if versionNum?[0] as! String != currentAppVersion?["CFBundleShortVersionString"] as! String {
                self.alertForUpdateApp()
            }
        }
    }

func alertForUpdateApp() {

    let alertController = UIAlertController(title: "Update Available", message: "There is a newer version of this app available", preferredStyle: .alert)
    let alertActionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    let alertActionUpdate = UIAlertAction(title: "Update", style: .default, handler: { _ in
        if let url = URL(string: Constants.API_REDIRECT_TO_ITUNES),
            UIApplication.shared.canOpenURL(url){
            UIApplication.shared.open(url, options: [:], completionHandler: nil)

        }
    })

    alertController.addAction(alertActionCancel)
    alertController.addAction(alertActionUpdate)

    let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
    let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]

    presentedViewController.present(alertController, animated: true, completion: nil)

}

我需要通过在我的应用程序中按下“评价”按钮来打开我的iPhone的应用商店。这样,我就需要在我的iPhone的应用商店中打开我的应用程序...您的代码如何实现?这是我的帖子链接:https://stackoverflow.com/questions/44499611/trigger-app-store-to-open-in-mobile-to-open-particular-app?noredirect=1#comment75992581_44499611 - hybrid Dev
1
请使用以下网址替代Constants.API_REDIRECT_TO_ITUNES: "https://itunes.apple.com/in/app/your-appName/id548615579?mt=8" - Raj Joshi
但是这段代码会触发应用商店或任何浏览器打开吗?因为我需要一个应用商店应用程序来打开并显示该应用程序。 - hybrid Dev
但在我的情况下,它是打开应用商店。 - Raj Joshi
我想提一下,iTunes查找可能存在缓存问题,这意味着它不会总是返回最新版本。防止这种情况的一种方法是在调用之前清除缓存:URLCache.shared.removeAllCachedResponses() - Janneman
显示剩余8条评论

3
Swift 4.2Xcode 10.2 中,您有两种打开 App StoreiTunes Store 的方法。
如果您想打开 App Store,请使用 https ,如果您想打开 iTunes Store,请使用 itms
例如:https://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //用于 App Store itms://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //用于 iTunes Store 方法1:这是一种简单、直接和旧的方法。
let url  = NSURL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?mt=8")//itms   https
    if UIApplication.shared.canOpenURL(url! as URL) {
        UIApplication.shared.openURL(url! as URL)
    }

方法二:新的方法

if let url = URL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?ls=1&mt=8") {
   if #available(iOS 10.0, *) {
       UIApplication.shared.open(url, options: [:], completionHandler: nil)
   } else {
       // Earlier versions
       if UIApplication.shared.canOpenURL(url as URL) {
          UIApplication.shared.openURL(url as URL)
       }
   }
}

3

针对那些寻找最新可行解决方案的人

这里提供的大部分解决方案都已过时和废弃,例如“canOpenURL”、“openURL”等。

let appStoreLink = "https://apps.apple.com/app/{app-name}/{app-id}"

guard let url = URL(string: appStoreLink) else { return }
UIApplication.shared.open(url)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接