显示InApp Purchase swift ios的年度价格

时间:2017-11-22 18:23:38

标签: ios swift3 in-app-purchase

目前我正在使用Swift开发iOS应用程序。我已启用InApp已购买。付款是完美的,但是当我显示来自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)"
        // ------------------------------------

        }
      }

2 个答案:

答案 0 :(得分:0)

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

你需要做这样的事情

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)

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

答案 1 :(得分:0)

非常感谢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!)"