【问题标题】:show yearly price in InApp Purchase swift ios在应用内购买 swift ios 中显示年度价格
【发布时间】:2018-05-06 14:17:32
【问题描述】:

目前我正在使用 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)"
        // ------------------------------------

        }
      }

【问题讨论】:

  • 不清楚你在问什么。您应该简单地显示在 iTunesConnect 中配置的价格,这就是您似乎正在做的事情。显示年度购买的月度价格是一种误导
  • 感谢 Paulw115 的回复。请看我编辑的问题。
  • 所以写代码来说明这一点。您需要将年度产品的价格除以 12。注意四舍五入,因为 $4.99 * 12 实际上是 $59.88,这就是为什么我说它有点误导。
  • 是的,但我找不到确切的代码。
  • 基本上是secondProd.price / 12

标签: ios swift3 in-app-purchase


【解决方案1】:

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

你需要做这样的事情

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 给了我宝贵的建议。改变后它对我有用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2022-01-23
    • 2018-12-26
    • 2015-05-08
    • 2020-02-17
    相关资源
    最近更新 更多