/storefront-assistant

Extra functionality for dealing with storefront identifiers in iOS

Primary LanguageSwiftThe UnlicenseUnlicense

Storefront Assistant

This is a simple class to assist in the conversion of a Storefront Identifier (from SKCloudServiceController) into a standard ISO country code for use with the iTunes Search API.

Installation

Just include the StorefrontAssistant.swift and StorefrontCountries.plist in your iOS 9.3 compatible project.

Example Usage

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.

Use case

If you want to either play Apple Music or save an Apple Music track to the user's media library, you will need to know which country they are in so you can find the relevant track identifier via the iTunes Search API. The only way to know which country the logged in user's Apple Music account is with is to use the SKCloudServiceController method requestStorefrontIdentifierWithCompletionHandler: which will give you an identifier to match up to the store. Using this class, you can do a simple compare against the list which was parsed from Apple's iTunes Affliate Links website.

Learn more

JSON Implementation

Loreto Parisi has put together some code in order to provide the plist file as a JSON; he also has a Node JS implementation to scrape this information regularly from the iTunes Affiliate Link Website. The StorefrontAssistant class could be easily converted to use this JSON file or you may want to convert his scraper to export to plist.