2017-04-14 165 views
4

我想在每次打開應用時檢查自動更新訂閱狀態。檢查自動更新訂閱是否仍然有效

這是爲了確保用戶仍然訂閱了該服務。我如何實現這一目標?

有什麼想法?謝謝

P.S:我使用SwiftyStoreKit

+1

您需要驗證收據的應用程序部分。請參閱Apple的應用內購買編程指南 – Paulw11

回答

2

下面是幾種方法可以做到收據驗證檢查的用戶授予訂閱。這裏有兩種方法可以正確地做到這一點:

  1. 在本地進行收據驗證,因爲它被寫入here
  2. 遠程執行收據驗證,因爲它的寫法爲here。有人提到,收據不應該發送到App Store白色的應用程序。簡短摘要:

    • 您的應用將收據發送到您的後端。
    • 您的後端將收據發送至Apple後端進行驗證。
    • 你的後端從蘋果獲得響應。
    • 您的後端將結果發送回您的應用程序的回執有效或無效。

在這兩種方式,你會得到應用內購買的名單。它也包含過期的訂閱。您將需要拋出所有訂閱並檢查到期日期。如果它仍然有效,您必須授予用戶訂閱權。

據我所知你使用的是SwiftyStoreKit,這裏是local receipt validation的開放任務。

+0

是的,我正在使用SwiftyStoreKit,但「SwiftyStoreKit.restorePurchases」不讓我檢查購買日期,這意味着我無法檢查恢復購買的有效期 – JayVDiyk

+0

是的。你是對的。以及每次執行restorePurchases認證(用戶名和密碼)對話框都會顯示給用戶。 – Ramis

+0

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

1

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



} 
}