检查自动更新订阅是否仍然有效

检查自动更新订阅是否仍然有效

问题描述:

我想在每次打开应用时检查自动更新订阅状态。检查自动更新订阅是否仍然有效

这是为了确保用户仍然订阅了该服务。我如何实现这一目标?

有什么想法?谢谢

P.S:我使用SwiftyStoreKit

+1

您需要验证收据的应用程序部分。请参阅Apple的应用内购买编程指南 – Paulw11

下面是几种方法可以做到收据验证检查的用户授予订阅。这里有两种方法可以正确地做到这一点:

  1. 在本地进行收据验证,因为它被写入here
  2. 远程执行收据验证,因为它的写法为here。有人提到,收据不应该发送到App Store白色的应用程序。简短摘要:

    • 您的应用将收据发送到您的后端。
    • 您的后端将收据发送至Apple后端进行验证。
    • 你的后端从苹果获得响应。
    • 您的后端将结果发送回您的应用程序的回执有效或无效。

在这两种方式,你会得到应用内购买的名单。它也包含过期的订阅。您将需要抛出所有订阅并检查到期日期。如果它仍然有效,您必须授予用户订阅权。

据我所知你使用的是SwiftyStoreKit,这里是local receipt validation的开放任务。

+0

是的,我正在使用SwiftyStoreKit,但“SwiftyStoreKit.restorePurchases”不让我检查购买日期,这意味着我无法检查恢复购买的有效期 – JayVDiyk

+0

是的。你是对的。以及每次执行restorePurchases认证(用户名和密码)对话框都会显示给用户。 – Ramis

+0

Ramis,所以,根本不可能得到那些restorePurchases的日期/有效性? – JayVDiyk

您可以使用此功能进行检查。它的作品与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)") 
    } 



} 
}