Ios 8, Swift, check validation for IAP

I am desperately trying to validate a receipt for an app in an IOS futur app written with Swift, IOS8.1

I'm using the Alamofire framework to compensate for an error in NSURLConnection for fast but still not getting a return from Apple Server.

func checkReceipt(data:NSData) {

    let ITMS_PROD_VERIFY_RECEIPT_URL        = "https://buy.itunes.apple.com/verifyReceipt"
    let ITMS_SANDBOX_VERIFY_RECEIPT_URL     = "https://sandbox.itunes.apple.com/verifyReceipt"

    let base64String = self.base64forData(data)
    if base64String != nil {

        let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(0))
        let receiptInformation = self.dictionaryFromPlistData(decodedData!)

        let payload:NSString = "{\"receipt-data\" : \"\(base64String)\"}"
        println("-----------------------------------------")
        println("payload \(payload)")
        println("-----------------------------------------")
        let payloadData:NSData = payload.dataUsingEncoding(NSUTF8StringEncoding)!

        let serverURL = NSURL(string: ITMS_SANDBOX_VERIFY_RECEIPT_URL) //ITMS_PROD_VERIFY_RECEIPT_URL;

        var request = NSMutableURLRequest(URL: serverURL!)    // (URL:
        request.HTTPMethod = "POST"
        request.HTTPBody = payloadData

        let (param, _) = Alamofire.ParameterEncoding.URL.encode(request, parameters: nil)

        Alamofire.request(param)
            .response { (request, response, data, error) in
                    println(request)
                    println(response)
                    println(error)
            }
    }
}

      

Server feedback:

Optional(<NSHTTPURLResponse: 0x17403b760> { URL: https://sandbox.itunes.apple.com/verifyReceipt } { status code: 200, headers {
    "Cache-Control" = "private, no-cache, no-store, no-transform, must-revalidate, max-age=0";
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 36;
    Date = "Tue, 11 Nov 2014 19:20:38 GMT";
    Expires = "Tue, 11 Nov 2014 19:20:38 GMT";
    "Set-Cookie" = "Pod=100; version=\"1\"; expires=Thu, 11-Dec-2014 19:20:38 GMT; path=/; domain=.apple.com, itspod=100; version=\"1\"; expires=Thu, 11-Dec-2014 19:20:38 GMT; path=/; domain=.apple.com, mzf_in=990212; version=\"1\"; path=/WebObjects; domain=.apple.com; secure; HttpOnly, mzf_dr=0; version=\"1\"; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/WebObjects; domain=.apple.com";
    "apple-timing-app" = "193 ms";
    "edge-control" = "no-store, cache-maxage=0";
    itspod = 100;
    pod = 100;
    "x-apple-application-instance" = nnnnnnnn;
    "x-apple-application-site" = SB;
    "x-apple-jingle-correlation-key" = xxxxxxxxx;
    "x-apple-lokamai-no-cache" = true;
    "x-apple-orig-url" = "http://sandbox.itunes.apple.com/WebObjects/MZFinance.woa/wa/verifyReceipt";
    "x-apple-translated-wo-url" = "/WebObjects/MZFinance.woa/wa/verifyReceipt";
    "x-frame-options" = SAMEORIGIN;
    "x-webobjects-loadaverage" = 0;
} })

      

Instead of receiving an application ...

Any help is appreciated

+3


source to share


1 answer


So. The problem is simple. You are looking at the response header instead of the body. View data. Also response

it is better to use method instead of method .responseJSON(options: [], completionHandler:)

, see my code



func validateReceipt() {
    if let receipt =  NSBundle.mainBundle().appStoreReceiptURL {
        if let data = NSData(contentsOfURL: receipt) {
            let requestContents:[String:String] = ["receipt-data":data.base64EncodedStringWithOptions([]), "password": "YOU_SHARED_SECRET"]
            let requestData = try! NSJSONSerialization.dataWithJSONObject(requestContents,options: [])
            let request = NSMutableURLRequest(URL: NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!)
            request.HTTPMethod = "POST"
            request.HTTPBody = requestData
            let (param, _) = Alamofire.ParameterEncoding.URL.encode(request, parameters: nil)

            Alamofire.request(param)
                .responseJSON(options: [], completionHandler: { (result) -> Void in
                    print(result)
                })
        }
    }
}

      

+1


source







All Articles