IAPs actually validating the receipt (Swift)

后端 未结 3 2045
时光取名叫无心
时光取名叫无心 2021-02-06 17:34

I have been trying to implement receipt validation in my spritekit game. I have been following various tutorial and basically ended up with this code

enum Reques         


        
3条回答
  •  星月不相逢
    2021-02-06 17:44

    Thats is where you return your receipt as JSON and can access it. ie

    if parseJSON["status"] as? Int == 0 {
        println("Sucessfully returned purchased receipt data")
    }
    

    Will tell you if you have successfully got the receipt because ["status"] 0 means its been returned ok

    You can further query and use the receipt data to find and use items from the JSON response. Here you can print the latest receipt info

    if let receiptInfo: NSArray = parseJSON["latest_receipt_info"] as? NSArray {
        let lastReceipt = receiptInfo.lastObject as! NSDictionary
        // Get last receipt
        println("LAST RECEIPT INFORMATION \n",lastReceipt)
    }
    

    Now you have your json data to use

    You can now query that data, in this example we are finding out when the subscription expires of an auto renew subscription form the JSOn response

    // Format date
    var formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    
    // Get Expiry date as NSDate
    let subscriptionExpirationDate: NSDate = formatter.dateFromString(lastReceipt["expires_date"] as! String) as NSDate!
    println("\n   - DATE SUBSCRIPTION EXPIRES = \(subscriptionExpirationDate)")
    

    Post the above code under

    if let parseJSON = json {
        println("Receipt \(parseJSON)")
    }
    

提交回复
热议问题