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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7666101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:44:57  来源:igfitidea点击:

Is it possible to get app id from iOS application programmatically?

objective-ciosapp-storeitunes

提问by breakp01nt

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

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

回答by kelin

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

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

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

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

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

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

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

回答by Gaurav Vaish

Use

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

Updating the answer with comment by @zaheer

用@zaheer 的评论更新答案

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

回答by Suhail Patel

You can use the Apple iTunes Link Makerto 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

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

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

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

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,

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

回答by Vimalkumar N.M.

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

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

回答by A Salcedo

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

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

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

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

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