如何在SKPayment中访问产品的价格?

时间:2010-05-24 01:33:34

标签: iphone xcode in-app-purchase

我正在为iPhone应用购买应用内购买。

我想在UILabel中以用户本地货币显示价格。为此,我需要价格&变量中的货币。

如何使用SKPayment获取包括货币在内的价格? (如果SKPayment正确用于此用途。)

我正在使用以下方式实例化产品:

SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"Identifier"];

提前感谢您的反馈!

5 个答案:

答案 0 :(得分:118)

使用NSLocaleCurrencySymbol + price.stringValue时出现问题:它不能处理不同语言环境的特殊性,例如。他们是否将货币符号放在前面。例如,挪威,丹麦,瑞典和瑞士都将货币放在后面。 17.00Kr。此外,大多数(?)欧洲国家使用','而不是'。'小数,例如。 “2,99€”而不是“€2.99”。

更好的计划是使用NSNumberFormatter。正如Ed所说,SKProduct返回的“priceLocale”是关键。它为NSNumberFormatter提供了正确格式化价格的智能。

通过使用Objective-C类别向SKProduct添加新属性,您还可以更轻松地实现这一目标。将以下两个文件添加到项目中:


SKProduct + priceAsString.h:

#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>

@interface SKProduct (priceAsString)
@property (nonatomic, readonly) NSString *priceAsString;
@end

SKProduct + priceAsString.m:

#import "SKProduct+priceAsString.h"

@implementation SKProduct (priceAsString)

- (NSString *) priceAsString
{
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
  [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
  [formatter setLocale:[self priceLocale]];

  NSString *str = [formatter stringFromNumber:[self price]];
  [formatter release];
  return str;
}

@end

然后,在您的代码中#import "SKProduct+priceAsString.h",您应该只能在代码中使用product.priceAsString

答案 1 :(得分:9)

确定任何信息的正确方法是使用SKProduct对象,该对象是在初始化{{SKProductResponse上调用- (void) start后从SKProductsRequest对象返回的。 1}}。像这样:

SKProductsRequest *req = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"Identifier"]];
req.delegate = self;
[req start];

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response {
    [request autorelease];
    if (response.products.count) {
        SKProduct *product = [response.products objectAtIndex:0];
        NSLocale *priceLocale = product.priceLocale;
        NSDecimalNumber *price = product.price;
        NSString *description = product.localizedDescription;
    }
}

答案 2 :(得分:2)

以下是使用extension

的上述答案的Swift版本
extension SKProduct {

    func priceAsString() -> String {

        let formatter = NSNumberFormatter()
        formatter.formatterBehavior = .Behavior10_4
        formatter.numberStyle = .CurrencyStyle
        formatter.locale = self.priceLocale
        return formatter.stringFromNumber(self.price)! as String
    }
}

答案 3 :(得分:2)

这是Swift 3.0的解决方案

extension SKProduct {
  func priceAsString() -> String {
    let formatter = NumberFormatter()
    formatter.formatterBehavior = .behavior10_4
    formatter.numberStyle = .currency
    formatter.locale = self.priceLocale
    return formatter.string(from: self.price)! as String
  }
}

答案 4 :(得分:0)

  • 适用于 Swift Objective-C
  • 强行展开从不你的朋友。
  • 使用计算属性为Objective-C场景创建更清晰的代码,因为它不需要[];)

    @objc公共扩展名SKProduct {

    var priceAsString: String? {
    
        let formatter = NumberFormatter()
        formatter.formatterBehavior = .behavior10_4
        formatter.numberStyle = .currency
        formatter.locale = self.priceLocale
        formatter.string(from: self.price)
    
        return formatter.string(from: self.price)
    }
    

    }

注意:不要忘记import StoreKit