Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd like to be able to determine which store the user connects to from inside my app, so that I can direct them to some appropriate content for their device AND store. Does anyone know how to get this information?

Basically, if the user is in the UK, and connects to the UK store, I want my function/method to return GB, if in Korea, I want KR, Australia = AU etc. Any help would be appreciated.

like image 413
pms1969 Avatar asked Mar 29 '10 19:03

pms1969


3 Answers

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];
}
like image 72
arteku Avatar answered Oct 02 '22 20:10

arteku


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

like image 34
jeko Avatar answered Oct 02 '22 19:10

jeko


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>
like image 2
Reimond Hill Avatar answered Oct 02 '22 19:10

Reimond Hill