I am having an In-App-Purchase for an iPhone app.
我正在为iPhone应用程序进行内购。
I want to display the price in the users local currency in a UILabel. For this I need the price & currency in a variable.
我想在UILabel中显示用户本地货币的价格。为此,我需要价格和货币作为变量。
How can I get the price including the currency using SKPayment? (If SKPayment is correct for this use.)
如何使用SKPayment获得包括货币在内的价格?(如果skpay用于此用途是正确的)
I'm instantiating the product using:
我使用:
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"Identifier"];
Thank you all in advance for your feedback!
感谢大家的反馈!
5 个解决方案
#1
118
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".
使用NSLocaleCurrencySymbol + price有一个问题。stringValue:它不能处理不同地区的特性,例如。他们是否把货币符号放在前面。挪威、丹麦、瑞典和瑞士都把他们的货币放在其后。17.00 kr。同时,大多数(?)欧洲国家用“,”而不是“。例如,为小数。而不是“99€€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.
更好的计划是使用NSNumberFormatter。正如Ed演示的那样,SKProduct返回的“priceLocale”是关键。它为NSNumberFormatter提供了正确格式化价格的智慧。
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:
您还可以通过使用Objective-C类别向SKProduct添加一个新属性来简化这一过程。向您的项目添加以下两个文件:
SKProduct+priceAsString.h:
SKProduct + priceAsString.h:
#import <Foundation/Foundation.h> #import <StoreKit/StoreKit.h> @interface SKProduct (priceAsString) @property (nonatomic, readonly) NSString *priceAsString; @end
SKProduct+priceAsString.m:
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.
然后,#“SKProduct + priceAsString进口。在你的代码中,你应该能够使用产品。priceAsString的代码。
#2
9
The correct way to determine any of that information is to use an SKProduct
object, retrieved from the SKProductResponse
object returned to the delegate after a call to - (void) start
on an initialized SKProductsRequest
. Something like this:
确定任何信息的正确方法是使用SKProduct对象,该对象在初始化SKProductResponse请求的- (void)启动后从SKProductResponse对象返回给委托。是这样的:
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;
}
}
#3
2
Here's the Swift version of the above answer using an 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
}
}
#4
1
Here is the solution for Swift 3.0
以下是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
}
}
#5
0
- Works in Swift and Objective-C
- 在Swift和Objective-C中工作。
- Force unwrapping is never your friend.
- 强行展开绝不是你的朋友。
-
Using computed properties creates cleaner code for Objective-C scenario because it doesn't need [] ;)
使用计算属性为Objective-C场景创建更清晰的代码,因为它不需要[];
@objc public extension SKProduct {
@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) }
}
}
NOTE : Don't forget import StoreKit
注意:不要忘记导入StoreKit
#1
118
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".
使用NSLocaleCurrencySymbol + price有一个问题。stringValue:它不能处理不同地区的特性,例如。他们是否把货币符号放在前面。挪威、丹麦、瑞典和瑞士都把他们的货币放在其后。17.00 kr。同时,大多数(?)欧洲国家用“,”而不是“。例如,为小数。而不是“99€€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.
更好的计划是使用NSNumberFormatter。正如Ed演示的那样,SKProduct返回的“priceLocale”是关键。它为NSNumberFormatter提供了正确格式化价格的智慧。
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:
您还可以通过使用Objective-C类别向SKProduct添加一个新属性来简化这一过程。向您的项目添加以下两个文件:
SKProduct+priceAsString.h:
SKProduct + priceAsString.h:
#import <Foundation/Foundation.h> #import <StoreKit/StoreKit.h> @interface SKProduct (priceAsString) @property (nonatomic, readonly) NSString *priceAsString; @end
SKProduct+priceAsString.m:
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.
然后,#“SKProduct + priceAsString进口。在你的代码中,你应该能够使用产品。priceAsString的代码。
#2
9
The correct way to determine any of that information is to use an SKProduct
object, retrieved from the SKProductResponse
object returned to the delegate after a call to - (void) start
on an initialized SKProductsRequest
. Something like this:
确定任何信息的正确方法是使用SKProduct对象,该对象在初始化SKProductResponse请求的- (void)启动后从SKProductResponse对象返回给委托。是这样的:
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;
}
}
#3
2
Here's the Swift version of the above answer using an 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
}
}
#4
1
Here is the solution for Swift 3.0
以下是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
}
}
#5
0
- Works in Swift and Objective-C
- 在Swift和Objective-C中工作。
- Force unwrapping is never your friend.
- 强行展开绝不是你的朋友。
-
Using computed properties creates cleaner code for Objective-C scenario because it doesn't need [] ;)
使用计算属性为Objective-C场景创建更清晰的代码,因为它不需要[];
@objc public extension SKProduct {
@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) }
}
}
NOTE : Don't forget import StoreKit
注意:不要忘记导入StoreKit