How to detect the active iTunes store on the iPhone/iPod Touch/iPad?

Since iOS 13.0, Apple introduced the SKStorefront API.

It allows you to check the current AppStore country the user is connected to.

SKStorefront: An object containing the location and unique identifier of an Apple App Store storefront.

Overview

In-app products you create through App Store Connect are available for sale in every region with an App Store. You can use the storefront information to determine the customer's region, and offer in-app products suitable for that region. You must maintain your own list of product identifiers and the storefronts in which you want to make them available.

Topics

  • countryCode: The three-letter code representing the country associated with the App Store storefront.
  • identifier: A value defined by Apple that uniquely identifies an App Store storefront.

https://developer.apple.com/documentation/storekit/skstorefront


So far you can use 2 methods with their pros and cons:

StoreFront

->SDK >= 13

->The three-letter code representing the country associated with the App Store storefront

if let storeFront = SKPaymentQueue.default().storefront{
    print("StoreFront CountryCode = ", storeFront.countryCode)
}
else {
    print("StoreFront NOT Available")
}

OR

SKCloudServiceController

-> Available from iOS 9.3.

-> Requires permission. On tvOS it can become messy as I can't find a way to change the permissions in settings...

-> Permission text quite confusing.

let skCloudServiceController = SKCloudServiceController()
SKCloudServiceController.requestAuthorization { (status) in
    guard status == .authorized else {
        return
    }

    skCloudServiceController.requestStorefrontCountryCode(completionHandler: { (countryCode, error)  in
        if let error = error {
            print("Failure: ", error)
        }
            else if let countryCode = countryCode {
            print("Country code: ", countryCode)
        }
    })
}

Don't forget to include that in your .plist file:

<key>NSAppleMusicUsageDescription</key>
<string>Store information</string>

The approach of getting the country code of the user's locale will work ... but only if the user's iTunes store is the same as their locale. This won't always be the case.

If you create an in-app purchase item, you can use Apple's StoreKit APIs to find out the user's actual iTunes country even if it's different from their device locale. Here's some code that worked for me:

- (void) requestProductData
{
    SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:
                                 [NSSet setWithObject: PRODUCT_ID]];
    request.delegate = self;
    [request start];
}

- (void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *myProducts = response.products;
    for (SKProduct* product in myProducts) {
        NSLocale* storeLocale = product.priceLocale;
        storeCountry = (NSString*)CFLocaleGetValue((CFLocaleRef)storeLocale, kCFLocaleCountryCode);
        NSLog(@"Store Country = %@", storeCountry);
    }

    [request release];

    // If product request didn't work, fallback to user's device locale
    if (storeCountry == nil) {
        CFLocaleRef userLocaleRef = CFLocaleCopyCurrent();
        storeCountry = (NSString*)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode);
    }

    // Now we're ready to start creating URLs for the itunes store
    [super start];
}