Check if an Auto-Renewable Subscription is still valid

对着背影说爱祢 提交于 2019-11-27 02:45:36

问题


I would like to check the Auto Renewable Subscription status whenever I open the app.

This is to make sure that the user is still subscribed to the service. How do I achieve this?

Any thoughts? Thank you

P.S.: I am using SwiftyStoreKit


回答1:


Here is several ways to do receipt validation to check is user granted to subscription. Here is two ways of doing it correctly:

  1. Do receipt validation locally as it is written here.
  2. Do receipt validation remotely as it is written here. It is mentioned that receipt should not be sent to App Store within an app. Short summary:

    • Your app sends receipt to your backend.
    • Your backend sends receipt to Apple backend for validation.
    • Your backend gets response from the apple.
    • Your backend sends result back to your app is receipt valid or invalid.

In both ways you will get list of in-app purchases. It will contain expired subscriptions as well. You would need to go through all subscriptions and check expiration dates. If it is still valid you must grant user with subscription.

As I understand you are using SwiftyStoreKit and here is open task for local receipt validation.




回答2:


You can check with this function. its works with swift4

func receiptValidation() {
let SUBSCRIPTION_SECRET = "yourpasswordift"
let receiptPath = Bundle.main.appStoreReceiptURL?.path
if FileManager.default.fileExists(atPath: receiptPath!){
    var receiptData:NSData?
    do{
        receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
    }
    catch{
        print("ERROR: " + error.localizedDescription)
    }
    //let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)

    print(base64encodedReceipt!)


    let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]

    guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
    do {
        let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
        let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"  // this works but as noted above it's best to use your own trusted server
        guard let validationURL = URL(string: validationURLString) else { print("the validation url could not be created, unlikely error"); return }
        let session = URLSession(configuration: URLSessionConfiguration.default)
        var request = URLRequest(url: validationURL)
        request.httpMethod = "POST"
        request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
        let task = session.uploadTask(with: request, from: requestData) { (data, response, error) in
            if let data = data , error == nil {
                do {
                    let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                    print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
                    // if you are using your server this will be a json representation of whatever your server provided
                } catch let error as NSError {
                    print("json serialization failed with error: \(error)")
                }
            } else {
                print("the upload task returned an error: \(error)")
            }
        }
        task.resume()
    } catch let error as NSError {
        print("json serialization failed with error: \(error)")
    }



}
}



回答3:


I wanted to provide an alternative solution that uses the RevenueCat SDK for those who still stumble upon this question.

AppDelegate.swift

Configure the RevenueCat Purchases SDK with your api key an optional user identifier.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    Purchases.configure(withAPIKey: "<...>", appUserID: "<...>")

    ...

    return true
}

Subscription status function

The function below checks the PurchaserInfo to see if the user still has an active "entitlement" (or you can check for an active product ID directly).

func subscriptionStatus(completion: @escaping (Bool)-> Void) {
    Purchases.shared.purchaserInfo { (info, error) in

        // Check if the purchaserInfo contains the pro feature ID you configured
        completion(info?.activeEntitlements.contains("pro_feature_ID") ?? false)

        // Alternatively, you can directly check if there is a specific product ID
        // that is active.
        // completion(info?.activeSubscriptions.contains("product_ID") ?? false)
    }
}

Getting subscription status

You can call the above function as often as needed, since the result is cached by the Purchases SDK it will return synchronously in most cases and not require a network request.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    subscriptionStatus { (subscribed) in
        if subscribed {
            // Show that great pro content
        }
    }
}

If you're using SwiftyStoreKit, the RevenueCat syntax is fairly similar and there is a migration guide available to help switch over.



来源:https://stackoverflow.com/questions/43408729/check-if-an-auto-renewable-subscription-is-still-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!