How to check In App Purchase Auto Renewable Subscription is valid

前端 未结 5 549
傲寒
傲寒 2020-12-13 00:15

I\'m looking to implement the new Auto Renewable subscriptions using In App purchase but I am unsure how or when to check if the user is currently subscribed. My understandi

5条回答
  •  Happy的楠姐
    2020-12-13 01:00

    Better to validate a receipt locally before making any calls to the Apple API. Every time the app runs it's a good practice to validate the local receipt and if you need to check whether user has any active subscriptions, you can retrieve all purchases from the local receipt and see if there is a purchase which is still active.

    I have implemented a small library written in Swift to simplify to work with In-App Receipt locally. You can easily fetch the object that represents the receipt (InAppReceipt) and retrieve an active purchase/all purchases.

    Feel free to use. Github link

    Here is an example of solving your problem:

    import TPInAppReceipt
    
    do {
        let receipt = try InAppReceiptManager.shared.receipt()
        
        //retrive active auto renewable subscription for a specific product and date
        let purchase = receipt.activeAutoRenewableSubscriptionPurchases(ofProductIdentifier: "ProductName", forDate: Date())
        
        //retrive all auto renewable subscription purchases for a specific product
        let allAutoRenewableSubscriptionPurchases = receipt.purchases(ofProductIdentifier: "productName").filter({ return $0.isRenewableSubscription })
    } catch {
        print(error)
    }
    

提交回复
热议问题