2013-01-24 90 views
1

我需要获取当前激活的iTunes商店国家代码。我已阅读有关获取当前语言环境的信息,但这不是非常智能的解决方案,因为用户可以拥有一个语言环境,但完全不同的iTunes帐户。解决方案不需要合法,苹果拒绝在这里不是问题。有没有人使用私有框架找到了这个场景的解决方案?获取当前iTunesStore国家

回答

2

您可以在通过检查产品的priceLocale之一上的NSLocale请求商店的产品后执行此操作。试试这个代码:

- (NSString*) storeCountryCode:(NSArray*) products 
{ 
    NSString* countryCode = nil; 
    SKProduct* product = [products objectAtIndex:0]; 
    if(product != nil) 
    { 
     NSLocale* storeLocale = product.priceLocale; 
     countryCode = (__bridge NSString*)CFLocaleGetValue((__bridge CFLocaleRef)storeLocale, kCFLocaleCountryCode); 
    } 
    return countryCode; 
} 

你可以从你的SKProductsRequestDelegate方法称之为:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response 
{ 
    NSString* storeCountryCode = [self storeCountryCode:response.products]; 
    //other product handling 
} 
+0

是的,这是解决方案,但该应用程序将与企业许可证生产,并inAppPurchase不允许,对不对? –

+0

在App Store中不存在企业应用程序,因此不能在应用程序购买中使用 – Daniel

0

可以 “CFLocaleGetValue” 把这种使用
NSString *aCountry=[getCurrentItunesStoreCountryFromProudct:myProudct];

-(NSString *)getCurrentItunesStoreCountryFromProudct:(SKProduct *)aProudct 
{ 
NSLocale* storeLocale = aProudct.priceLocale; 
NSString *storeCountry = (NSString*)CFLocaleGetValue((CFLocaleRef)storeLocale, kCFLocaleCountryCode); 
return storeCountry; 
} 
0

您可以获取Storefront ID与此代码。由于它依赖私有API,因此不要在生产代码中使用。

NSError *error = nil; 
BOOL loaded = [[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/iTunesStore.framework"] loadAndReturnError:&error]; 
if (loaded) { 
    NSString *localStoreFrontID = nil; 
    @try { 
     localStoreFrontID = [NSClassFromString(@"ISClient") valueForKeyPath:@"currentClient.localStoreFrontID"]; 
    } 
    @catch (NSException *exception) { 
     NSLog(@"%@", exception); 
    } 

    NSLog(@"localStoreFrontID: %@", localStoreFrontID); 
} 
else { 
    NSLog(@"Error: %@", error); 
} 

在iOS 5.1.1上,为我打印localStoreFrontID: 143459-2,2,即瑞士商店。我不确定-2,2后缀是什么意思,也许是关于语言。我没有在iOS 6上进行过测试。

请注意,它在您通过设置→存储退出帐户后仍然有效。

+0

我在运行6.1的设备上测试过。 localStoreFrontID返回null。 – XCool