IAP(在应用程序购买中)介绍价格如何管理iOS



我有一个成功实现了iOS的IAP介绍价格的集合,但是我对iOS方面是否需要任何额外代码来管理它感到困惑吗?如果是这样,需要什么?

好吧,如果要正确实现,实际上需要额外的代码。这是为了将特定的报价带给客户,对他的收获有清晰的了解。

iOS 12和13,您获得3种不同类型的订阅介绍价格:

  • 免费试用
  • 您去付款
  • 付款

有关这三个的重要性以及订阅的简单且整体的视图,请参阅具有建议的UI

的自动可再生订阅Apple Doc

出示您购买视图控制器时,您应该:

  1. 正确显示要约:例如:" 免费开始您的1个月免费审判,然后每月$ 1.99 "免费试用价格;
  2. 处理以下事实:用户过去已经使入门价格受益,因此显示" 订阅价格为$ 1.99/月"(继续以相同的示例(。这就需要您通过用户拥有的收据,并检查设置为true的" IS_TRIAL_PERIOD"或" IS_IN_INTRO_OFFER_PERIOD"(请参阅答案底部的我的代码(。
  3. >

在我之前提到的Apple文档中,有许多"实际"屏幕显示出不同的用例。

我的班级处理每个产品的入门价格

/**
 Receipt description
 */
public class LatestReceipt: CustomStringConvertible {
    public var description: String {
        let prefix = type(of: self)
        return """
        (prefix) expires (expiredDate?.compact(timeStyle: .medium) ?? "nil"):
        (prefix) • product: (productID);
        (prefix) • isEligible: (isEligible);
        (prefix) • isActive: (isActive);
        (prefix) • have been in free trial: (haveBeenInFreeTrialPeriod);
        (prefix) • intro date: (introOfferPeriodDate?.compact(timeStyle: .medium) ?? "nil");
        (prefix) • free trial period: (inFreeTrialPeriod).
        """
    }

    /// Related product ID
    var productID = ""

    /**
     Tells whether the user is eligible for any introductory period
     - Note: From the Apple Doc: You can use “is_in_intro_offer_period” value to determine if the user is eligible for introductory pricing. If a previous subscription period in the receipt has the value “true” for either the “is_trial_period” or “is_in_intro_offer_period” keys, the user is not eligible for a free trial or introductory price within that subscription group.
     */
    public var isEligible = true
    /// When did the user benefited the intro offer? Nil means never.
    public var introOfferPeriodDate: Date? = nil
    /// When did the user benefited the free trial? Nil means never.
    public var freeTrialPeriodDate: Date? = nil
    public var inFreeTrialPeriod = false
    /// Tells whether the user benefited the free trial period in the past
    public var haveBeenInFreeTrialPeriod = false
    /// Is still active for today?
    public var isActive = false
    /// Last expired date
    public var expiredDate: Date? = nil
    // Latest receipt content
    public var content = NSDictionary()
    /**
     Replaces the current receipt if the new one is more recent. And compute isActive and others propteries.
     - Parameters:
        - receipt: The new receipt to test.
     */
    public func replaceIfMoreRecent(receipt: NSDictionary) {
        // Replace receipt if more recent
        if let ed = expiredDate {
            // -- Expiring product (aka subscriptions): add only the most recent
            guard let receiptExpiresDate = receipt["expires_date"] as? String,
                let red = StoreKit.getDate(receiptExpiresDate) else {
                print("(#function): *** Error unable to get receipt expiredDate or convert it to a Date(), skipped")
                return
            }
            // This receipt is most recent than the currently stored? if, yes, replace
            if red.isAfter(ed) {
                content = receipt
                expiredDate = red

                // Is in trial?
                if let tp = content["is_trial_period"] as? String {
                    inFreeTrialPeriod = tp == "true"
                    if inFreeTrialPeriod {
                        haveBeenInFreeTrialPeriod = true
                    }
                }
                // When was the intro?
                if let intro = content["is_in_intro_offer_period"] as? String,
                    intro == "true" {
                    introOfferPeriodDate = red
                }

                let now = Date()
                // Subscription still active today?
                isActive = red.isAfterOrSame(now)
                // Eligibility; check against PREVIOUS subscription period
                if !isActive {
                   // You can use “is_in_intro_offer_period” value to determine if the user is eligible for introductory pricing. If a previous subscription period in the receipt has the value “true” for either the “is_trial_period” or “is_in_intro_offer_period” keys, the user is not eligible for a free trial or introductory price within that subscription group.
                    let benefitedFromIntroductoryPrice = introOfferPeriodDate != nil || haveBeenInFreeTrialPeriod
                    isEligible = !benefitedFromIntroductoryPrice
                }
            }
            print("(#function): new (self)")
        }
    }

    init(receipt: NSDictionary) {
        content = receipt
        if let productID = receipt["product_id"] as? String {
            self.productID = productID
        }
        // When subscription, set expires date
        if let receiptExpiresDate = receipt["expires_date"] as? String,
            let red = StoreKit.getDate(receiptExpiresDate) {
            expiredDate = red
            // Is in trial?
            if let tp = content["is_trial_period"] as? String,
                tp == "true" {
                inFreeTrialPeriod = true
                haveBeenInFreeTrialPeriod = true
            }
            // When was the intro?
            if let intro = content["is_in_intro_offer_period"] as? String,
                intro == "true" {
                introOfferPeriodDate = red
            }

            let now = Date()
            // Subscription still active today?
            isActive = red.isAfterOrSame(now)
            // Eligibility; check against PREVIOUS subscription period
            if !isActive {
                // You can use “is_in_intro_offer_period” value to determine if the user is eligible for introductory pricing. If a previous subscription period in the receipt has the value “true” for either the “is_trial_period” or “is_in_intro_offer_period” keys, the user is not eligible for a free trial or introductory price within that subscription group.
                let benefitedFromIntroductoryPrice = introOfferPeriodDate != nil || inFreeTrialPeriod
                isEligible = !benefitedFromIntroductoryPrice
            }
        }
    }
}

如果正确实施了IAP入门价格,则不需要在iOS方面做任何额外的工作来管理它。价格通过iTunesConnect界面进行管理。通过正确实施它,我的意思是使用SKProduct API来获取价格并在应用程序中显示它,以便当您从iTunes Connect更改它时,它也会在应用程序中更改。

最新更新