2017-04-14 16 views
4

私は、アプリケーションを開くたびに自動更新サブスクリプションのステータスを確認したいと思います。自動更新可能なサブスクリプションが有効かどうかを確認してください

これは、ユーザーが依然としてサービスに加入していることを確認するためです。これをどのように達成するのですか?

どのような考えですか?ここで

P.S:私はSwiftyStoreKit

+1

とその作品あなたは領収書のアプリセクションに検証する必要があります。 Apple – Paulw11

答えて

2

を使用していますが、チェックするために領収書の検証を行うには、いくつかの方法でいただき、ありがとうございますと、サブスクリプションに付与されたユーザーです。

  1. 領収書の検証は、hereと書かれているようにローカルで行います。
  2. hereと書かれているように、リモートで領収証の検証を行います。レシートをApp Storeのホワイトアプリに送るべきではないと言われています。短い要約:

    • あなたのアプリはバックエンドに領収書を送信します。
    • お客様のバックエンドは、確認のために領収書をAppleバックエンドに送信します。
    • バックエンドはAppleからの応答を受け取ります。
    • あなたのバックエンドはあなたのアプリに結果を送り返し、領収書の有効または無効を通知します。両方の方法で

あなたは、アプリ内購入のリストを取得します。期限切れのサブスクリプションも同様に含まれます。すべてのサブスクリプションを投げ、有効期限を確認する必要があります。それでも有効な場合は、ユーザーにサブスクリプションを付与する必要があります。

私はあなたがSwiftyStoreKitを使用していることを理解しており、ここにはlocal receipt validationのオープンタスクがあります。

+0

のアプリの購入プログラミングガイドを参照してください。はい、私はSwiftyStoreKitを使用していますが、SwiftyStoreKit.restorePurchasesは購入日を確認することができません。つまり、復元された購入の有効性をチェックすることができません – JayVDiyk

+0

はい。あなたは正しいです。同様に、restorePurchases認証(ユーザー名とパスワード)ダイアログを実行するたびに、ユーザーにダイアログが表示されます。 – Ramis

+0

ラミス、そう、それらの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)") 
    } 



} 
}