Ben Dodson

Freelance iOS, Apple Watch, and Apple TV Developer

Storefront Additions: A simple way to convert SKCloudServiceController store identifiers into country codes

In my article a few days ago, I noted that SKCloudServiceController would give you a storefront identifier but that there was very little information about how to use that identifier when using the iTunes Search API. As it turns out, Apple do provide a list of store identifiers with their respective ISO country codes on their iTunes Affiliate Links website - a big thank you to Loreto Parisi for directing me to it.

In order to make things a bit easier for other developers, I’ve put together a simple class and plist file that will automatically return a country code for the current user’s store. It’s incredibly simple:

StorefrontAssistant.countryCode { (countryCode, error) in
    if let error = error {
        NSLog("Error: \(error)")
    }
    if let countryCode = countryCode {
        NSLog("Country code: \(countryCode)")
    }
}

Storefront Assistant will automatically request permissions to access the Media Library and then fetch the current storefront identifier for the logged in user. If there are any issues (i.e. permissions denied, no connectivity, country code can’t be found) then an NSError will be returned.

Once you have the country code, you can then use the iTunes Search API to search for tracks in the correct country; these can then be used with MPMusicPlayerController for immediate playback or MPMediaLibrary for saving to the user’s Media Library.

The full code is available on GitHub.

Update, 10th Mar 2016: Loreto Parisi has put together some code in order to provide this list as a JSON file instead of a plist; he also has a Node JS implementation to scrape this information regularly from the iTunes Affiliate Link Website. Check it out on GitHub.

Update, 31st Mar 2016: Thanks to Matt Abras it turns out that the above code isn’t necessary; you can just use the undocumented s=143444 instead of country=gb when using the iTunes Search API.

The Divide #9 - Board Game Tangents » « Details on the iOS 9.3 Media Library additions