温馨提示:本文翻译自stackoverflow.com,查看原文请点击:ios - How to determine if a user has been subscribed before

ios - 如何确定用户之前是否已订阅

发布于 2020-03-27 12:08:18

我希望能够确定用户是否曾经被订阅过,以便能够更改购买按钮的名称。换句话说,在开始时该按钮将说,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
    }
}

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

查看更多

查看更多

提问者
fs_tigre
被浏览
62
enc_life 2019-07-04 00:47

您可以选中该对象allPurchasedProductIdentifiers NSSet<NSString *>上的,RCPurchaserInfo以查看该用户购买的所有产品标识符,而与到期日期无关。

或者,如果要专门检查“ 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,Web等)上解锁,因此iOS上的按钮可能实际上并未触发还原。

发布
问题

分享
好友

手机
浏览

扫码手机浏览