ios - 带有价格等级文本的 iAP 购买按钮

标签 ios swift storekit

如何将 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/

相关文章:

ios - 在 iOS 9.3/Xcode 7.3 中使用 StoreKit 常量时使用未解析的标识符

ios - 尝试访问 Swift 中的无主引用总是导致应用程序崩溃

ios - 当我在调试中放置它时,Xcode 8 不显示任何变量值

xcode - 使用 Swift 在 UIScrollView 中居中图像

swift - SwiftUI 中的 Picker 委托(delegate)滚动方法

swift - 我想在我的开关盒中运行这两个功能,但只运行一个;我该如何解决?

ios - 什么会使UIViewController暂时变得无响应?

ios - UIActivityViewController 仅适用于 Facebook、Twitter 和 Instagram

ios - 在 IOS 中为框架创建 bundle ?

javascript - 混淆移动用户可点击的下拉菜单