在应用内购买 swift ios 中显示年度价格

man*_*809 2 in-app-purchase ios swift3

目前我正在使用 Swift 开发 iOS 应用程序。我已启用应用内购买。付款进展顺利,但当我显示来自 iTunes Connect 的数据时,每月订阅数据显示得很好,但我希望以每月格式在年度选项卡上显示价格,并提供一些折扣,当用户点击卡时,它应该显示年度价格。我无法做到这一点。描述如图所示。提前致谢。

图片1

图2

我想要这样的展示价格

// 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)"
        // ------------------------------------

        }
      }
Run Code Online (Sandbox Code Playgroud)

Jac*_*ing 8

以月为单位显示年度订阅比仅除以十二稍微复杂一些。SKProduct.price是一个NSDecimalNumber类,而不是常规浮点数,因此标准除法运算符不起作用。

你需要做这样的事情

product.price.dividing(by: NSDecimalNumber(decimal: Decimal(12.0)))
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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