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

发布于 2024-12-08 09:21:25 字数 71 浏览 1 评论 0原文

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

Is there any way for getting appstore id from running iOS application? (For asking user to rate it and providing link to appstore.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

苯莒 2024-12-15 09:21:25

是的。您无法离线获取应用商店应用 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< /a>

Yes, it is. You can't get appstore app id (called Apple ID in iTunes Connect) offline, but you can request it using iTunes Search Api. Download the context of the following link:

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

You will get a JSON response, containing "trackId":YOUR_APP_ID key-value. Try it in your browser!

My answer is based on the another answer: https://stackoverflow.com/a/11626157/3050403

迟到的我 2024-12-15 09:21:25

使用

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

@zaheer 的评论更新答案

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

Use

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

Updating the answer with comment by @zaheer

Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String
寄居者 2024-12-15 09:21:25

您可以使用 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技巧:

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

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

You can use the Apple iTunes Link Maker to search for your App (or any app for that matter) and get your App Store URL. From there you could get your App URL which will remain the same for your App and put it in your Application. When you use itms:// instead of http://, it will directly open in the App Store app directly

For example, if I wanted to use the Twitter App URL, the link maker tells me the URL is:

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

Now do the itms trick:

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

And it will directly open in the App Store than redirect via Safari then to the App Store,

请爱~陌生人 2024-12-15 09:21:25

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

You can use this below function.
Note: This API is not documented in Apple.

//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()
}
与风相奔跑 2024-12-15 09:21:25

这是 Vimalkumar NM 的出色答案的稍微改进版本,用于从 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)
    }
}

Here's a slightly improved version of Vimalkumar N.M.'s great answer for fetching the App Store tracking ID from Apple themselves, updated for Swift 5, with two improvements:

  1. It simplifies the error handling a bit—either your callback gets the final App Store URL or nil.
  2. It provides the official App Store base URL (rather than the trackID, from which you would need to build the URL yourself)

The final URL you're handed (in the non-error case, of course) will look something like:
https://apps.apple.com/us/app/[app-name]/id123456789?uo=4

(See the bottom of this answer for turning that URL into the review 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()
}

Now, to go from the "base" App Store URL to the URL you'd use to manually initiate a review, you'll need to append the query string action=write-review. Here's an extension you can add to URL to support this in a clean way (you certainly wouldn't want to just add "&action=write-review" to the string version of the URL, since it may or may not already include a query parameter!):

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

Then you can add to use it like this:

fetchAppStoreUrl() { appStoreUrl in
    if let reviewUrl = appStoreUrl?.appending(queryParameter: "action", value: "write-review") {
        UIApplication.shared.open(reviewUrl, options: [:], completionHandler: nil)
    }
}
不美如何 2024-12-15 09:21:25

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

get the app id like this

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
    
}
奢华的一滴泪 2024-12-15 09:21:25

对于 appId,您只需按组件分隔包标识符字符串,然后只需获取最后一个组件即可。

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

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

For the appId only you need to separate the bundle identifier string by components and simply grab the last component.

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

Or handle the full string accordingly but it will have the entire bundle identifier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文