Check if auto-renewable subscription is valid

I want to check the status of an automatic renewable subscription whenever I open the application.

This is to ensure that the user is still subscribing to this service. How to achieve this?

Any thoughts? thank you

PS: I am using SwiftyStoreKit

+11
source share
3 answers

Here are a few ways to check receipts to check if a user is being subscribed. Here are two ways to do it right:

  1. Confirm the receipt locally, as written here .
  2. Confirm receipt remotely as written here . It is mentioned that a receipt should not be sent to the App Store by a white application. Summary:

    • Your application sends a receipt to your backend.
    • Your backend sends a check to the Apple backend for verification.
    • Your backend receives a response from the apple.
    • Your backend sends the result back to your application, the check is valid or invalid.

In both cases, you will receive a shopping list in the application. It will also contain expired subscriptions. You will need to go through all the subscriptions and check the expiration dates. If it is still valid, you must provide the user with a subscription.

As I understand it, you are using SwiftyStoreKit, and here is an open task for checking a local receipt .

+5
source

You can check this feature. his work 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 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)") } } } 
+5
source

I wanted to offer an alternative solution that uses Profitable SDK for those who are still faced with this issue.

AppDelegate.swift

Configure the RevenueCat SDK for purchases with your API key and optional user ID.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { Purchases.configure(withAPIKey: "<...>", appUserID: "<...>") ... return true } 

Subscription Status Feature

The function below checks the PurchaserInfo parameter to see if the user has an active β€œright” (or you can directly check the active product identifier).

 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) } } 

Obtaining a subscription status

You can call the above function as often as necessary, since the result is cached by the SDK for purchases, in most cases it will be returned synchronously and does 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 use SwiftyStoreKit, the syntax of RevenueCat is pretty similar, and there is a migration guide to help switch.

+2
source

Source: https://habr.com/ru/post/1266732/


All Articles