How to access the price of a product in SKPayment?

前端 未结 5 1650
無奈伤痛
無奈伤痛 2020-12-23 02:41

I am having an In-App-Purchase for an iPhone app.

I want to display the price in the users local currency in a UILabel. For this I need the price & currency in a

5条回答
  •  天涯浪人
    2020-12-23 03:17

    • Works in Swift and Objective-C
    • Force unwrapping is never your 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)
      }
      

      }

    NOTE : Don't forget import StoreKit

提交回复
热议问题