xcode 如何在 SKPayment 中访问产品的价格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2894321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the price of a product in SKPayment?
提问by favo
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 获取包括货币在内的价格?(如果 SKPayment 适合此用途。)
I'm instantiating the product using:
我正在使用以下方法实例化产品:
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"Identifier"];
Thank you all in advance for your feedback!
提前感谢大家的反馈!
回答by gid
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 氪。此外,大多数(?)欧洲国家使用“,”而不是“。” 对于小数,例如。“2,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
#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
#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.
然后,#import "SKProduct+priceAsString.h"
在您的代码中,您应该能够product.priceAsString
在代码中使用。
回答by Ed Marty
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) start
初始化后返回给委托的对象中检索的SKProductsRequest
。像这样的东西:
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;
}
}
回答by rebello95
Here's the Swift version of the above answer using an extension
:
这是使用 Swift 版本的上述答案extension
:
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
}
}
回答by Reza Shirazian
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
}
}
回答by Darkwonder
- Works in Swiftand Objective-C
- Force unwrapping is neveryour friend.
Using computed properties creates cleaner code for Objective-C scenario because it doesn't need [] ;)
@objc public extension 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) }
}
- 适用于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) }
}
NOTE : Don't forget import StoreKit
注意:不要忘记 import StoreKit