我希望能够确定用户是否曾经被订阅过,以便能够更改购买按钮的名称。换句话说,在开始时该按钮将说,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
}
}
检查用户之前是否已订阅的最佳方法是什么?
您可以选中该对象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上的按钮可能实际上并未触发还原。
@ enc_life-您能否澄清一下您的第二个建议?您正在说明以下内容... 如果的
activeEntitlements
值为零,并且有购买日期,则可以假定它是先前购买的,然后已过期。但是在您的代码中,我们仅检查是否purchaseDate
为否,nil
而不是检查权利是否为nil。抱歉,如果我没有发现明显的问题。@fs_tigre我更新了我的答案以包含完整的代码示例。让我知道,这一点更加清楚。
知道了,非常感谢您的澄清。