28

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

breakp01nt
  • 613
  • 1
  • 8
  • 13

7 Answers7

21

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

gvaish
  • 9,374
  • 3
  • 38
  • 43
kelin
  • 11,323
  • 6
  • 67
  • 104
10

Use

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

Updating the answer with comment by @zaheer

Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String
LunaCodeGirl
  • 5,432
  • 6
  • 30
  • 36
gvaish
  • 9,374
  • 3
  • 38
  • 43
  • 2
    **Swift**: `NSBundle.mainBundle().infoDictionary?["CFBundleIdentifier"] as? NSString` – Zaheer Nov 19 '14 at 06:58
  • 20
    guys how can you rate through the bundle id ?! he is asking about the appstore app id ! – Moataz Hossam Jun 11 '15 at 10:34
  • I upvoted because this is what I came here to find out. It is unfortunate that the OP's question wasn't very specific, and (probably) meant the other thing. (Which AFAIK is not referred to anywhere as an App ID.) – livingtech Jun 30 '15 at 21:05
  • 2
    could also use `kCFBundleIdentifierKey` since its a constant:) – Daniel Galasko Aug 19 '15 at 12:34
5

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,

Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
  • 2
    With link maker you can make link to some specific application. What I'm asking about is programmatically getting id from current application (to avoid creating link for each application manually). – breakp01nt Oct 05 '11 at 18:56
  • I don't think it's possible to do that programmatically. You only need to set the URL once per application because the ID stays the same whenever you submit updates. All the solutions i've come in contact with need you to manually specify the App ID so I don't think it's possible. – Suhail Patel Oct 05 '11 at 19:44
  • You can also programmatically HTTP the link maker from within your app and parse the HTML results. – hotpaw2 Oct 05 '11 at 19:45
4

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()
}
Vimalkumar N.M.
  • 493
  • 2
  • 4
  • 16
2

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)
    }
}
s3cur3
  • 2,749
  • 2
  • 27
  • 42
1

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
    
}
Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
-1

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.

A Salcedo
  • 6,378
  • 8
  • 31
  • 42