代码之家  ›  专栏  ›  技术社区  ›  pms1969

如何在iPhone/iPod Touch/iPad上检测活动的iTunes商店?

  •  21
  • pms1969  · 技术社区  · 16 年前

    我希望能够从我的应用程序中确定用户连接到哪个商店,这样我就可以引导他们找到适合他们设备和商店的内容。有人知道如何获得这些信息吗?

    5 回复  |  直到 16 年前
        1
  •  34
  •   arteku    14 年前

    获取用户所在地区的国家代码的方法将起作用。。。但前提是用户的iTunes商店与其所在地区相同。情况并非总是这样。

    如果你创建了一个应用内购买项目,你可以使用苹果的storekitapi找出用户实际所在的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];
    }
    
        2
  •  3
  •   jeko    6 年前

    SKStorefront

    它允许您检查用户连接到的当前AppStore国家/地区。

    概述

    话题

    • :表示与应用商店店面关联的国家/地区的三个字母代码。
    • 标识符 :Apple定义的唯一标识应用商店店面的值。

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

        3
  •  2
  •   Reimond Hill    6 年前

    到目前为止,您可以使用两种方法,各有利弊:

    店面

    -&燃气轮机;SDK>=13

    -&燃气轮机;这个 表示与应用商店店面关联的国家/地区的代码

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

    或者

    -&燃气轮机;可从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>
    
        4
  •  0
  •   alex    16 年前

        5
  •  0
  •   Costique    16 年前
        6
  •  0
  •   Community Mohan Dere    9 年前

    我需要同样的功能。目前,我正在考虑使用NSLocale中的数据作为默认值进行读取,但在settings.app中添加一个设置,以便用户在不匹配时进行自定义。

    此函数取自 an answer to another question of mine .

    - (NSString *)getUserCountry
    {
        NSLocale *locale = [NSLocale currentLocale];
        return [locale objectForKey: NSLocaleCountryCode];
    }
    
        7
  •  -4
  •   chancyWu Jack BeNimble    11 年前

    你应该用

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

    这将返回一个类似en\u US或en\u UK或en\u AU甚至zh\u CN、zh\u MY、jp\u jp等语言代码。

    分析您支持的正确代码并相应地指导它们。

    推荐文章