带有价格层文本的iAP购买按钮

时间:2015-12-27 13:52:48

标签: 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)
        }
    }

当我尝试这个时,它就崩溃了: let validProduct: SKProduct = response.products[0]

我已导入StoreKit并在课程中添加了SKProductsRequestDelegate, SKPaymentTransactionObserver

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您需要使用SKProductsRequest

  • 使用相关的产品标识符
  • 对其进行初始化
  • 设置delegate
  • start
  • 在委托中,实现productsRequest:didReceiveResponse:,它将接收与标识符匹配的产品。然后,您可以从产品中检索价格

请注意,您应使用货币样式的数字格式化程序,使用提供的区域设置来正确格式化价格。请参阅SKProduct class Combatant a where health :: a -> Int attack :: a -> Int defense :: a -> Int damage :: a -> Int -> a data Person = Person { personHP :: Int } deriving Show data Monster = Monster { monsterHP :: Int } deriving Show -- We will assume for time that your HP determines your attack. instance Combatant Person where health = personHP attack = personHP defense _ = 0 damage (Person hp) dmg = Person (hp - dmg) instance Combatant Monster where health = monsterHP attack = monsterHP defense _ = 0 damage (Monster hp) dmg = Monster (hp - dmg) fight :: (Combatant a, Combatant b) => a -> b -> (a, b) fight fighter1 fighter2 = let damageTo1 = attack fighter2 - defense fighter1 damageTo2 = attack fighter1 - defense fighter2 in (damage fighter1 damageTo1, damage fighter2 damageTo2) 属性说明中的示例(仅在&#34时可见; Objective-C"模式,但很容易转换为Swift)。