I would like to be able to determine if a user has ever been subscribed to
be able to change the name of the purchase button. In other words, at start the button will say, Try It Now
if the user is currently subscribed the button will say Subscribed
but if the user used to be subscribed but the subscription has been expired I want to display Renew
on the purchase button.
Currently everything works except for the Renew
option.
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
}
}
What would be the best way to check if a user has been subscribed before?
You can check the allPurchasedProductIdentifiers NSSet<NSString *>
on the RCPurchaserInfo
object to see all the product identifiers the user has purchased regardless of expiration date.
Alternatively, if you want to check for the "myKillerFeatureEntitlement" entitlement specifically, you can check the purchaseDateForEntitlement
property. If the activeEntitlements
is nil and there is a purchase date you can assume it was previously purchased then expired.
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
}
}
}
}
Note that the entitlement could have been unlocked on another platform (Android, web, etc.) so the button on iOS may not actually be triggering a restore.