如何在SKPayment中获取产品的价格? [英] How to access the price of a product in SKPayment?

查看:706
本文介绍了如何在SKPayment中获取产品的价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



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

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

提前感谢您的反馈!

解决方案

使用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







'p> SKProduct + priceAsString.m:




 #进口 SKProduct + priceAsString.h 

@implementation SKProduct(priceAsString)

- (的NSString *)priceAsString
{
NSNumberFormatter *格式化= [[NSNumberFormatter的alloc] INIT];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[self priceLocale]];

NSString * str = [formatter stringFromNumber:[self price]];
[格式化程序发布];
return str;
}

@end




< hr>

然后, #importSKProduct + priceAsString.h在你的代码中,你应该只能使用 product.priceAsString 代码。


I am having an In-App-Purchase for an iPhone app.

I want to display the price in the users local currency in a UILabel. For this I need the price & currency in a variable.

How can I get the price including the currency using SKPayment? (If SKPayment is correct for this use.)

I'm instantiating the product using:

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

Thank you all in advance for your feedback!

解决方案

There's a problem with just using NSLocaleCurrencySymbol + price.stringValue: it doesn't handle the peculiarities of different locales, eg. whether they put the currency symbol in front or not. Norway, Denmark, Sweden and Switzerland all put their currency after, eg. 17.00Kr. Also, most(?) European countries use ',' instead of '.' for decimals, eg. "2,99 €" rather than "€2.99".

A better plan is to use NSNumberFormatter. The "priceLocale" that the SKProduct returned, as Ed demonstrated, is the key. It gives NSNumberFormatter the smarts to format a price correctly.

You can also make this a lot easier by adding a new property to SKProduct using an Objective-C category. Add the following two files to your project:


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


Then, #import "SKProduct+priceAsString.h" in your code, and you should just be able to use product.priceAsString in code.

这篇关于如何在SKPayment中获取产品的价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆