【问题标题】:Open AppStore through button通过按钮打开 AppStore
【发布时间】:2014-11-11 00:56:21
【问题描述】:

你们能帮我把下面的代码翻译成 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?)

提前致谢!

【问题讨论】:

标签: ios swift xcode cocoa-touch app-store


【解决方案1】:

这里。但我强烈建议你学习 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://apple.com/app/id839686104") {
    UIApplication.shared.open(url)
}

【讨论】:

  • 这是否有效地允许用户将应用“赠送”给第三方? x-gift 引用?
  • 哈,X-Gift 是我的应用程序名称@KatherineJenkins
  • 是否需要“id”或者我可以只使用带数字的猿吗?
  • 是否可以打开非商店应用程序?像内部开发的一样?
  • 仅供参考,这将打开 iTunes Store 应用而不是 App Store(那么它应该是 itms-apps://
【解决方案2】:

Swift 3 语法并通过 'if let' 进行了改进

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

2017 年 7 月 5 日更新(感谢 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)
    }
}

【讨论】:

  • 不错,但不推荐使用 openURL()。现在我们应该将这个用于相同的行为 UIApplication.shared.open(url, options: [:], completionHandler: nil)
  • 您现在还可以在 url 字符串中添加 {action=write-review} - 直接跳转到评分和撰写评论...
【解决方案3】:

我使用这个组合,它更适合价格/购物。

(部分来自here

    @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 更好
【解决方案4】:

带有完成处理程序的 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() 是做什么的?
  • 在这种情况下,我正在调用我的分析提供商来添加用户打开应用商店的记录。但我只是删除了这条线,因为它不相关。感谢您的提问!
【解决方案5】:

由于其他答案对我不起作用(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!)
}

【讨论】:

    【解决方案6】:

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

    【讨论】:

      【解决方案7】:

      在 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 的应用商店中打开我的应用程序......我该如何处理你的代码......这里是我的帖子......stackoverflow.com/questions/44499611/…
      • 你应该整合这段代码: if let url = URL(string: Constants.API_REDIRECT_TO_ITUNES), UIApplication.shared.canOpenURL(url){ UIApplication.shared.open(url, options: [:],完成处理程序:无)}
      • 使用这个网址代替 Constants.API_REDIRECT_TO_ITUNES: "itunes.apple.com/in/app/your-appName/id548615579?mt=8"
      • 但是这段代码是否会触发应用商店打开或任何浏览器打开...因为我需要一个应用商店应用来打开并显示应用
      • 它将打开应用商店应用程序。
      【解决方案8】:

      如果你想在应用商店打开使用

       let appstoreUrl =  "https://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
       UIApplication.shared.openURL(URL(string: appstoreUrl)!)
      

      如果你想在iTunes商店使用

        let ituneUrl =  "itms-apps://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
       UIApplication.shared.openURL(URL(string: ituneUrl)!)
      

      【讨论】:

        【解决方案9】:

        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

        方法一:这是简单直接的老方法

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

        方法2:新方法

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

        【讨论】:

          【解决方案10】:

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

          【讨论】:

          • 什么是标识符字符串?
          • @YogeshPatel 它是来自应用程序 url 的应用程序 ID,下面说是 url,那么应用程序 id 将是 123456789 apps.apple.com/us/app/id123456789
          【解决方案11】:

          对于新的 AppStore,只需在 AppStore 上打开您应用的链接并将https 方案替换为itms-apps 方案。 Swift 4 示例:

          if let url = URL(string: "itms-apps://itunes.apple.com/us/app/my-app/id12345678?ls=1&mt=8") {
              UIApplication.shared.open(url, options: [:], completionHandler: nil)
          }
          

          您可以在应用信息页面上找到您应用的链接。

          【讨论】:

            【解决方案12】:

            斯威夫特 5

            let url = "your app url"
            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)
                }
            }
            

            【讨论】:

              【解决方案13】:

              对我来说,这个答案之一没有奏效。 (Swift 4) 应用总是打开 iTunes Store 而不是 AppStore。

              我必须将网址更改为“http://appstore.com/%Appname%”,如本苹果问答中所述:https://developer.apple.com/library/archive/qa/qa1633/_index.html

              例如这样

              private let APPSTORE_URL = "https://appstore.com/keynote"
              UIApplication.shared.openURL(URL(string: self.APPSTORE_URL)!)
              

              (从应用名称中删除空格)

              【讨论】:

                【解决方案14】:

                SwiftUI

                import StoreKit
                
                struct StoreView: UIViewControllerRepresentable {
                
                    let appID: String
                 
                     
                    func makeUIViewController(context: UIViewControllerRepresentableContext<StoreView>) -> SKStoreProductViewController {
                        let sKStoreProductViewController = SKStoreProductViewController()
                        let parameters = [ SKStoreProductParameterITunesItemIdentifier : appID]
                        sKStoreProductViewController.loadProduct(withParameters: parameters)
                        return sKStoreProductViewController
                    }
                
                    func updateUIViewController(_ uiViewController: SKStoreProductViewController, context: UIViewControllerRepresentableContext<StoreView>) {
                
                    }
                
                }
                

                //如何使用

                .sheet(isPresented: $showAppAtStore){
                StoreView(appID: "12345678") 
                }
                

                【讨论】:

                • 如果你想这样做来更新你自己的应用程序,但是,这在某些版本的 iOS 中会失败,因为应用程序在加载时无法更新。可惜。
                • 这将失败 -SKStoreProductViewController 无法嵌入;你将收到 SwiftUI 的断言崩溃
                【解决方案15】:
                    var url  = NSURL(string: "itms-apps://itunes.apple.com/app/id1024941703")
                    if UIApplication.sharedApplication().canOpenURL(url!) == true  {
                        UIApplication.sharedApplication().openURL(url!)
                    }
                

                Xcode 6.4

                【讨论】:

                • 这与上述答案相同,但 URL 不同。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2014-11-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-11-05
                • 1970-01-01
                相关资源
                最近更新 更多