是否可以通过编程方式从iOS应用程序获取app id?

时间:2011-10-05 18:45:54

标签: objective-c ios app-store itunes

有没有办法让appstore id运行iOS应用程序? (要求用户对其进行评级并提供指向appstore的链接。)

7 个答案:

答案 0 :(得分:15)

使用

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

通过@zaheer评论更新答案

NSBundle.mainBundle().infoDictionary?["CFBundleIdentifier"] as? NSString

答案 1 :(得分:10)

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

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

您将获得包含"trackId":YOUR_APP_ID键值的JSON响应。在浏览器中试试吧!

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

答案 2 :(得分:3)

您可以使用Apple iTunes Link Maker搜索您的应用(或任何相应的应用)并获取您的App Store网址。从那里,您可以获得您的应用程序URL,它将与您的应用程序保持相同并将其放入您的应用程序中。当您使用itms://而不是http://时,它将直接在App Store应用程序中直接打开

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

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

现在做itms技巧:

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

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

答案 3 :(得分:1)

您可以使用以下功能。 注意: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()
}

答案 4 :(得分:1)

这是一个稍微改进的 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

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

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

答案 5 :(得分:0)

对于appId,您只需要按组件分隔捆绑标识符字符串,然后只需抓取最后一个组件。

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

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

答案 6 :(得分:0)

像这样获取应用程序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
    
}