iOS 8,Swift,IAP的收据验证 [英] ios 8, Swift, receipt validation for IAP

查看:111
本文介绍了iOS 8,Swift,IAP的收据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拼命尝试验证用Swift,IOS8.1编写的未来IOS应用程序中的应用程序内收据

i'm desperately trying to validate a receipt for in-app in a futur IOS application written with Swift, IOS8.1

我正在使用Alamofire框架来补偿NSURLConnection中的错误,以便迅速但仍不能从Apple服务器获得任何回报。

I'm using the Alamofire framework to compensate a bug in NSURLConnection for swift but still not having any return from the 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)
            }
    }
}

服务器的反馈是:

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;
} })

代替申请收据...

感谢任何帮助

推荐答案

所以。您的问题很简单。您可以在响应标头上看到正文。观看数据。代替 response 方法,最好使用 .responseJSON(options:[],completeHandler:)方法,请参见我的代码

So. You problem is simple. You watch on response header instead body. Watch data. Also instead response method better use .responseJSON(options: [], completionHandler:) method, 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)
                })
        }
    }
}

这篇关于iOS 8,Swift,IAP的收据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆