如何将 iAP 的价格以文本形式添加到按钮上?
我的意思不是这样的:
upgradeButton.setTitle("19", forState: .Normal)
但如果我在 iTunes Connect 中更改价格,按钮上的价格也会发生变化。我尝试过这样的事情:
var product_id: NSString?;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
product_id = "com.iia.cuz.PRO";
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
let response = SKProductsResponse()
let validProduct: SKProduct = response.products[0]
if (validProduct.productIdentifier == self.product_id) {
upgradeButton.setTitle(validProduct.price.description, forState: .Normal)
}
}
当我尝试这个时,它崩溃了:
让 validProduct: SKProduct = response.products[0]
我已导入 StoreKit
并在类中添加 SKProductsRequestDelegate、SKPaymentTransactionObserver
。
有什么建议吗?
最佳答案
您需要使用 SKProductsRequest
:
- 使用相关产品标识符进行初始化
- 设置
委托(delegate)
开始
它- 在委托(delegate)中,实现
productsRequest:didReceiveResponse:
,它将接收与标识符匹配的产品。然后您可以从产品中检索价格
请注意,您应该使用货币样式的数字格式化程序,并使用提供的区域设置来正确设置价格格式。请参阅 price
的描述中的示例SKProduct
的属性(仅在“Objective-C”模式下可见,但易于转换为 Swift)。
关于ios - 带有价格等级文本的 iAP 购买按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481074/