我希望能够从我的应用程序中确定用户连接到哪个商店,以便可以将他们引导到适合其设备和商店的某些内容。有人知道如何获取这些信息吗?
基本上,如果用户在英国,并连接到英国商店,则我希望我的函数/方法返回GB;如果在韩国,则返回KR;澳大利亚= AU,等等。任何帮助将不胜感激。
我希望能够从我的应用程序中确定用户连接到哪个商店,以便可以将他们引导到适合其设备和商店的某些内容。有人知道如何获取这些信息吗?
基本上,如果用户在英国,并连接到英国商店,则我希望我的函数/方法返回GB;如果在韩国,则返回KR;澳大利亚= AU,等等。任何帮助将不胜感激。
获取用户所在地区的国家代码的方法是可行的...但只有当用户的iTunes商店与其所在地区相同时才有效。这种情况并不总是发生。
如果您创建了一个应用内购买项目,您可以使用Apple的StoreKit API来查找用户的实际iTunes国家,即使它与他们的设备所在地不同。以下是一些适用于我的代码:
- (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];
}
https://developer.apple.com/documentation/storekit/skstorefront
到目前为止,您可以使用两种方法来实现IT技术相关内容,它们分别是:
StoreFront
->SDK >= 13
->代表与App Store storefront相关联的国家的三个字母代码
if let storeFront = SKPaymentQueue.default().storefront{
print("StoreFront CountryCode = ", storeFront.countryCode)
}
else {
print("StoreFront NOT Available")
}
OR
SKCloudServiceController
-> 从iOS 9.3开始可用。
-> 需要获得权限。在tvOS上可能会变得混乱,因为我找不到在设置中更改权限的方法...
-> 权限文本相当令人困惑。
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)
}
})
}
不要忘记在您的 .plist 文件中包含这个内容:
<key>NSAppleMusicUsageDescription</key>
<string>Store information</string>
我需要相同的功能。目前我考虑使用NSLocale中的数据作为默认值进行读取,但是在设置应用程序中添加一个设置,以便用户可以自定义此设置,如果默认值不匹配。
这个函数来自于我的另一个问题的答案。
- (NSString *)getUserCountry
{
NSLocale *locale = [NSLocale currentLocale];
return [locale objectForKey: NSLocaleCountryCode];
}
getUserCountry
方法是否会返回美国用户的US? - prendio2[NSLocale currentLocale]
并不能给你iTunes商店的地区,它只能给你设备的地区,而这两者是不同的。事实上,我使用了[NSLocale currentLocale]
并因此遇到了错误,直到我通过读取SKProduct的priceLocale
属性来获取正确的商店地区时才解决了问题。我住在澳大利亚,但仍在使用美国iTunes帐户,所以对我来说这两个地区也是不同的。 - Erik van der Neut我建议您尝试使用iTunes 深度链接。例如,http://itunes.com/apps/appname 可以将用户带到本地应用商店,在那里她可以花钱购买。
[[userDefaults dictionaryRepresentation] objectForKey:@"NSLocaleCode"];
这将返回类似于en_US、en_UK、en_AU,甚至是zh_CN、zh_MY、jp_JP等语言代码。
解析您支持的正确代码并相应地进行引导。
product.priceLocale
读取iTunes商店货币代码的想法绝对可行。 - Jonas Sourlier