在 InApp 购买中显示年度价格 swift ios

show yearly price in InApp Purchase swift ios

目前我正在使用 Swift 开发 iOS 应用程序。我已启用 InApp Purchased。付款做得很好,但是当我显示来自 iTunes Connect 的数据时,月度订阅数据显示得很好,但我想在年度选项卡上以月度格式显示价格并有一些折扣,当用户点击卡片时,它应该显示年度价格。我无法做到这一点。说明如图。提前致谢。

image1

image 2

I want show price like this

// Currently i am getting product info with this method

  // MARK: - REQUEST IAP PRODUCTS
func productsRequest (_ request:SKProductsRequest, didReceive response:SKProductsResponse) {
    if (response.products.count > 0) {
        iapProducts = response.products
       // showHUD("Loading...")

        let indexPath = IndexPath.init(row: 0, section: 0)
        guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell else { return }

       let numberFormatter = NumberFormatter()

        let firstProduct = response.products[0] as SKProduct

        print("localizedDescription", firstProduct.localizedDescription)
         print("localizedTitle", firstProduct.localizedTitle)


        // Get its price from iTunes Connect

        numberFormatter.formatterBehavior = .behavior10_4
        numberFormatter.numberStyle = .currency
        numberFormatter.locale = firstProduct.priceLocale
        let price1Str = numberFormatter.string(from: firstProduct.price)

        // Show its description
        cell.monthlyLabel.text = "\(firstProduct.localizedTitle)"
        cell.rupeesLabel.text = "\(price1Str!)"
         cell.perMonthLabel.text = "\(firstProduct.localizedDescription)"

        let indexPath1 = IndexPath.init(row: 1, section: 0)
        guard let cell2 = collectionView.cellForItem(at: indexPath1) as? CollectionViewCell else { return }

        let secondProd = response.products[1] as SKProduct

        // Get its price from iTunes Connect
        numberFormatter.locale = secondProd.priceLocale
        let price2Str = numberFormatter.string(from: secondProd.price)

        // Show its description
        cell2.monthlyLabel.text = "\(secondProd.localizedTitle)"
        cell2.rupeesLabel.text = "\(price2Str!)"
        cell2.perMonthLabel.text = "\(secondProd.localizedDescription)"
        // ------------------------------------

        }
      }

按月显示年度订阅比仅除以十二要复杂一些。 SKProduct.priceNSDecimalNumber class,不是常规浮点数,因此标准除法运算符不起作用。

你需要做这样的事情

product.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)))

这将为您提供分割后的 NSDecimalNumber,您可以将其传递给格式化程序。一个问题是除后的值可能四舍五入到一个不正确的值。诀窍是创建一个自定义的 NSDecimalNumberHandler,根据需要四舍五入。

let behavior = NSDecimalNumberHandlerroundingMode: .down, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
product.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)), withBehavior: behavior)

这应该为您提供了以月率显示年价格所需的所有控制。我还建议您在附近显示总价,以免误导用户太多。优化您的购买流程和试图欺骗他人之间存在微妙的界限。

非常感谢 Jacob Eiting。这在 Swift3 中对我有用。

let behavior = NSDecimalNumberHandler(roundingMode: .down, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)

         let price2Str = numberFormatter.string(from: (secondProd.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)), withBehavior: behavior)))

        cell2.rupeesLabel.text = "\(price2Str!)"