【问题标题】:Is it possible to get app id from iOS application programmatically?是否可以以编程方式从 iOS 应用程序获取应用程序 ID?
【发布时间】:2011-12-01 17:45:23
【问题描述】:

有什么方法可以从正在运行的 iOS 应用程序中获取 appstore id? (用于要求用户对其评分并提供指向应用商店的链接。)

【问题讨论】:

    标签: objective-c ios app-store itunes


    【解决方案1】:

    是的,是的。您无法离线获取 appstore 应用 ID(在 iTunes Connect 中称为 Apple ID),但您可以使用 iTunes Search Api请求它。下载以下链接的上下文:

    http://itunes.apple.com/lookup?bundleId=YOUR_APP_BUNDLE_ID
    

    您将收到一个 JSON 响应,其中包含 "trackId":YOUR_APP_ID 键值。在您的浏览器中试一试!

    我的答案是基于另一个答案: https://stackoverflow.com/a/11626157/3050403

    【讨论】:

      【解决方案2】:

      使用

      NSString* appID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
      

      用@zaheer 的评论更新答案

      Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String
      

      【讨论】:

      • 斯威夫特NSBundle.mainBundle().infoDictionary?["CFBundleIdentifier"] as? NSString
      • 伙计们,您如何通过捆绑 ID 进行评分?!他在询问应用商店的应用 ID!
      • 我投了赞成票,因为这是我来这里寻找的。不幸的是,OP的问题不是很具体,并且(可能)意味着另一件事。 (AFAIK 在任何地方都没有被称为 App ID。)
      • 也可以使用kCFBundleIdentifierKey,因为它是一个常量:)
      【解决方案3】:

      您可以使用Apple iTunes Link Maker 搜索您的应用(或任何应用)并获取您的应用商店 URL。从那里您可以获得您的应用程序 URL,该 URL 对于您的应用程序将保持不变并将其放入您的应用程序中。当你使用itms://而不是http://时,会直接在App Store应用中直接打开

      例如,如果我想使用 Twitter 应用 URL,链接制作者会告诉我 URL 是:

      http://itunes.apple.com/us/app/twitter/id333903271?mt=8&uo=4
      

      现在开始使用它的技巧:

      itms://itunes.apple.com/us/app/twitter/id333903271?mt=8&uo=4
      

      它会直接在 App Store 中打开,而不是通过 Safari 重定向到 App Store,

      【讨论】:

      • 使用链接生成器,您可以链接到某些特定的应用程序。我要问的是以编程方式从当前应用程序获取 id(以避免手动为每个应用程序创建链接)。
      • 我认为不可能以编程方式做到这一点。您只需为每个应用程序设置一次 URL,因为每次提交更新时 ID 都保持不变。我接触的所有解决方案都需要你手动指定App ID,所以我认为不可能。
      • 您还可以在您的应用程序中以编程方式 HTTP 链接生成器并解析 HTML 结果。
      【解决方案4】:

      您可以使用以下功能。 注意:Apple 中没有记录此 API。

      //MARK: To get app iTunes id and app name 
      
      func getAppDetailsFromServer() {
      
          var bundleIdentifier: String {
              return Bundle.main.infoDictionary?["CFBundleIdentifier"] as! String
          }
          let baseURL: String = "http://itunes.apple.com/lookup?bundleId=\(bundleIdentifier)"
          let encodedURL = baseURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
      
          // Creating URL Object
          let url = URL(string:encodedURL!)
      
          // Creating a Mutable Request
          var request = URLRequest.init(url: url!)
      
          //Setting HTTP values
          request.httpMethod = "GET"
          request.timeoutInterval = 120
      
          let configuration = URLSessionConfiguration.default
      
          let session = URLSession(configuration: configuration)
      
          let downloadTask = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
      
              //API Call over,getting Main queue
              DispatchQueue.main.async(execute: { () -> Void in
      
                  if error == nil
                  {
      
                      if data != nil {
      
                          do {
      
                              let resultDictionary:NSDictionary! = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary
      
                              if resultDictionary != nil && resultDictionary.count > 0 {
                                  if (resultDictionary.object(forKey: "results") as! NSArray).count != 0 {
                                      let AppName = "\(((resultDictionary.object(forKey: "results") as! NSArray).object(at: 0) as! NSDictionary).object(forKey: "trackCensoredName")!)"
                                      let AppId = "\(((resultDictionary.object(forKey: "results") as! NSArray).object(at: 0) as! NSDictionary).object(forKey: "trackId")!)"
                                      print("AppName : \(AppName) \nAppId: \(AppId)")
                                  }
                              } else {
                                  print("Unable to proceed your request,Please try again")
                              }
                          } catch {
                              print("Unable to proceed your request,Please try again")
      
                          }
                      } else {
                          print("Unable to proceed your request,Please try again")
                      }
                  } else {
                      print("Unable to proceed your request,Please try again")
                  }
              })
      
          })
          downloadTask.resume()
      }
      

      【讨论】:

        【解决方案5】:

        这是Vimalkumar N.M.'s great answer 的略微改进版本,用于从 Apple 自己获取 App Store 跟踪 ID,针对 Swift 5 进行了更新,有两个改进:

        1. 它稍微简化了错误处理——您的回调获取最终的 App Store URL 或 nil
        2. 它提供官方 App Store 基本 URL(而不是 trackID,您需要自己构建 URL)

        您收到的最终 URL(当然,在非错误情况下)将类似于: https://apps.apple.com/us/app/[app-name]/id123456789?uo=4

        (请参阅此答案的底部以将该 URL 转换为评论 URL。)

        func fetchAppStoreUrl(completionHandler: @escaping (URL?) -> Void) {
            guard let bundleId = Bundle.main.infoDictionary?[kCFBundleIdentifierKey as String] as? String,
                  let urlEncodedBundleId = bundleId.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
                  let lookupUrl = URL(string: "http://itunes.apple.com/lookup?bundleId=\(urlEncodedBundleId)") else {
                completionHandler(nil)
                return
            }
            
            let session = URLSession(configuration: .default)
            let downloadTask = session.dataTask(with: URLRequest(url: lookupUrl), completionHandler: { (data, response, error) -> Void in
                DispatchQueue.main.async() {
                    if error == nil,
                       let data = data,
                       let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
                       let results = json["results"] as? [[String: Any]],
                       !results.isEmpty,
                       let trackViewUrl = results.first?["trackViewUrl"] as? String {
                        completionHandler(URL(string: trackViewUrl))
                    } else {
                        completionHandler(nil)
                    }
                }
            })
            downloadTask.resume()
        }
        

        现在,要从“基本”App Store URL 转到您用于手动发起审核的 URL,您需要附加查询字符串 action=write-review。这是您可以添加到URL 的扩展,以干净的方式支持此功能(您当然不想将"&action=write-review" 添加到URL 的字符串版本,因为它可能已经包含查询参数,也可能不包含查询参数! ):

        extension URL {
            func appending(queryParameter: String, value: String? = nil) -> URL? {
                if var components = URLComponents(url: self, resolvingAgainstBaseURL: false) {
                    components.queryItems = (components.queryItems ?? []) + [URLQueryItem(name: queryParameter, value: value)]
                    return components.url
                }
                return nil
            }
        }
        

        然后你可以像这样添加使用它:

        fetchAppStoreUrl() { appStoreUrl in
            if let reviewUrl = appStoreUrl?.appending(queryParameter: "action", value: "write-review") {
                UIApplication.shared.open(reviewUrl, options: [:], completionHandler: nil)
            }
        }
        

        【讨论】:

          【解决方案6】:

          像这样获取应用ID

          guard let bundleID = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String else {
              return
          }
          
          let url = "https://itunes.apple.com/lookup?bundleId=\(bundleID)"
          
          Alamofire.request(url).responseJSON { response in
              guard let value = response.result.value else { return }
              let json = JSON(value)  // from: import SwiftyJSON
              let storeVersion = json["results"][0]["version"].stringValue
          
              let storeProductID = json["results"][0]["trackId"].intValue
              // result: 1506562619
              
          }
          

          【讨论】:

            【解决方案7】:

            仅对于 appId,您需要将包标识符字符串按组件分开,然后简单地抓取最后一个组件。

            NSString appId = [[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"] componentsSeparatedByString:@"."] lastObject];
            

            或相应地处理完整的字符串,但它将具有整个捆绑标识符。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-11-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-04-23
              • 2012-04-11
              相关资源
              最近更新 更多