【问题标题】:Correct in app-purchasing local currency symbol在应用程序购买本地货币符号中更正
【发布时间】:2014-04-12 16:45:52
【问题描述】:

通过应用内购买,我希望当地货币符号明显适合用户当地货币。使用 NSLocaleCurrencySymbol 作为检测用户位置的主要来源是否安全。这是我的部分代码:

         NSLocale *theLocale = [NSLocale currentLocale];
     NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
     NSString *cost = [NSString stringWithFormat:@"%@",product.price];

默认情况下,当用户购买 iOS 设备时,每台设备的国际设置中会自动设置正确的区域格式??

【问题讨论】:

    标签: ios in-app-purchase currency nslocale


    【解决方案1】:

    你想使用NSNumberFormatter

    Objective-C

    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setLocale:product.priceLocale]; 
    NSString *cost = [formatter stringFromNumber:product.price];
    

    斯威夫特

    let formatter = NSNumberFormatter()
    formatter.numberStyle = .CurrencyStyle
    formatter.locale = product.priceLocale
    let cost = formatter.stringFromNumber(product.price)
    

    这将使用正确的十进制、分隔符和货币符号格式化货币。

    【讨论】:

    • 有没有办法对此进行测试,因为如果我更改 iphone 上的国际设置,我的应用内购买总是显示英国 (£) 货币符号??
    • 它使用SKProduct 的区域设置(以及货币)而不是设备,所以你实际上是在测试它:)
    • 好的,有没有办法测试货币符号?
    • 您正在以某种方式对其进行测试,就好像您更改了国际设置并且它不会更改它正在使用的货币符号一样。我想你可能会找到一个国际设置,它的货币也使用英镑,然后 UI 可能会显示 GBP 而不是显示 £ 会使用户感到困惑。比如美元和澳元。
    • 在不同的商店创建一个测试帐户(您可以在开设测试帐户时设置国家)。这样你就可以看到货币是否正常。
    【解决方案2】:

    更新到 Swift 3.0 并作为 SKProduct 的扩展:

    import StoreKit
    
    extension SKProduct {
        func getLocalizedPrice()->String{
            let formatter = NumberFormatter()
            formatter.numberStyle = .currency
            formatter.locale = self.priceLocale
            if let formated = formatter.string(from: self.price) {
                return formated
            } else {
                return "\(self.price)"
            }
        }
    }
    

    【讨论】:

    • 好答案。请记住,格式化程序的 string(from: Number) 方法返回一个可选项,因此您需要指定 String? 作为返回类型或在返回之前强制解包。
    【解决方案3】:

    你不需要这样做,SKProduct自带了一个本地的用户存储,你可以使用-

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:product.priceLocale];
    NSString *formattedString = [numberFormatter stringFromNumber:product.price];
    

    看这里-

    Apple Docs

    【讨论】:

    • 感谢您的帮助 - 但无论我将 iPhone 设置为何种区域格式,它始终显示为我的当地货币 (£)。例如,在我的 iPhone 上,如果我将区域格式设置为美国 - 当我测试我的应用内购买时,它仍然显示为 £(磅)而不是 $ 美元??
    • 是的,因为 SKProduct 的格式是由用户登录的 App Store 国家/地区设置的。如果您尝试自己设置本地价格,则会向用户显示错误的价格。测试的方法是在其他应用商店开一个测试账户并检查币种。
    【解决方案4】:

    迅速

    var currency_format = NSNumberFormatter()
    currency_format.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currency_format.locale = validProduct.priceLocale
    mylabel.text = currency_format.stringFromNumber(validProduct.price)
    

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2011-06-21
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多