如何判断用户之前是否订阅过 - RevenueCat

How to determine if a user has been subscribed before - RevenueCat

我希望能够确定用户是否订阅过 能够更改购买按钮的名称。换句话说,在开始时按钮会说,Try It Now 如果用户当前订阅了按钮会说 Subscribed 但如果用户曾经订阅但订阅已过期我想显示 Renew 在购买按钮上。

目前除 Renew 选项外,一切正常。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    Purchases.shared.purchaserInfo { (purchaserInfo, error) in
        self.configurePurchases(purchaserInfo: purchaserInfo)
    }
}

func configurePurchases(purchaserInfo: PurchaserInfo?) {
    if let purchaserInfo = purchaserInfo {
        if purchaserInfo.activeEntitlements.contains("myKillerFeatureEntitlement") {
            myButton.setTitle("Subscribed",for: .normal)
            labelAutoTaxDescription.text = "You are currently subscribed. Thank you for your support."

            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .medium

            if let expirationDate = purchaserInfo.expirationDate(forEntitlement: "myKillerFeatureEntitlement") {
                self.labelAutoTaxDetectionPrice.text = "Expiration Date: \(dateFormatter.string(from: expirationDate))"
            }
        }
    }

    // Here is where I need check if it's a returning user so I can change the name of the button
    if isReturningUser{

        myButton.setTitle("Renew",for: .normal)

        // other code
    }
}

检查用户之前是否订阅过的最佳方法是什么?

您可以检查 RCPurchaserInfo 对象上的 allPurchasedProductIdentifiers NSSet<NSString *> 以查看用户已购买的所有产品标识符,无论到期日期如何。

或者,如果您想具体检查 "myKillerFeatureEntitlement" 权利 ,您可以检查 purchaseDateForEntitlement 属性。如果 activeEntitlements 为零并且有购买日期,您可以假设它是之前购买的,然后过期了。

func configurePurchases(purchaserInfo: PurchaserInfo?) {
    if let purchaserInfo = purchaserInfo {
        if purchaserInfo.activeEntitlements.contains("myKillerFeatureEntitlement") {
            myButton.setTitle("Subscribed",for: .normal)
            labelAutoTaxDescription.text = "You are currently subscribed. Thank you for your support."

            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .medium

            if let expirationDate = purchaserInfo.expirationDate(forEntitlement: "myKillerFeatureEntitlement") {
                self.labelAutoTaxDetectionPrice.text = "Expiration Date: \(dateFormatter.string(from: expirationDate))"
            }

        // Here is where I need check if it's a returning user so I can change the name of the button
        } else if purchaserInfo.purchaseDate(forEntitlement: "myKillerFeatureEntitlement") != nil {

                myButton.setTitle("Renew",for: .normal)

                // other code
            }
        }
    }
}

请注意,该权利可能已在其他平台(Android、网络等)上解锁,因此 iOS 上的按钮实际上可能不会触发恢复。