开发者

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

开发者 https://www.devze.com 2022-12-24 20:31 出处:网络
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 thei开发者_如何学JAVAr device AND store.Does anyone kno

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 thei开发者_如何学JAVAr 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.


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];
}


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>


A hard way to get this function is to set up up a single app for every app store country. Each app holds it's own country store information. This assumes, that a user sticks to one store, which should be true for most people.


I suggest you try iTunes deep links. For example, http://itunes.com/apps/appname should take the user to the local App Store where she spends money.


I need the same functionality. At the moment I'm considering reading using data from NSLocale as default, but adding a setting in settings.app for user to customise this if it does not match.

This function is taken from an answer to another question of mine.

- (NSString *)getUserCountry
{
    NSLocale *locale = [NSLocale currentLocale];
    return [locale objectForKey: NSLocaleCountryCode];
}


You should probably use

[[userDefaults dictionaryRepresentation] objectForKey:@"NSLocaleCode"];

This will return a language code like en_US or en_UK or en_AU or even zh_CN, zh_MY, jp_JP and so on.

Parse the correct codes which you support and direct them accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消