我想写一些代码来验证我的iOS应用程序的订阅。我下面这个教程:http://savvyapps.com/blog/how-setup-test-auto-renewable-subscription-ios-app/如何解决错误:“无效的JSON写入类型(NSConcreteData)”
这不是在雨燕2.0,所以我不得不转换代码的一些,但我有这个线故障:
let requestData = try! NSJSONSerialization.dataWithJSONObject(receiptDictionary, options: NSJSONWritingOptions.PrettyPrinted) as NSData!
当它碰到这条线,它打印此错误消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteData)'
这里的整体功能:
func validateReceipt() {
print("Validating")
if let receiptPath = NSBundle.mainBundle().appStoreReceiptURL?.path where NSFileManager.defaultManager().fileExistsAtPath(receiptPath) {
print("Loading Validation")
let receiptData = NSData(contentsOfURL:NSBundle.mainBundle().appStoreReceiptURL!)
print(receiptData)
let receiptDictionary = ["receipt-data" :
receiptData!.base64EncodedDataWithOptions([]), "password" : "placeholder"]
let requestData = try! NSJSONSerialization.dataWithJSONObject(receiptDictionary, options: NSJSONWritingOptions.PrettyPrinted) as NSData!
let storeURL = NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!
let storeRequest = NSMutableURLRequest(URL: storeURL)
storeRequest.HTTPMethod = "POST"
storeRequest.HTTPBody = requestData
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(storeRequest, completionHandler: { (data, response, connection) -> Void in
if let jsonResponse: NSDictionary = try! NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as?
NSDictionary, let expirationDate: NSDate = self.formatExpirationDateFromResponse(jsonResponse) {
print(expirationDate)
//self.updateIAPExpirationDate(expirationDate)
}
})
}
}
我之前没有做过应用内购买,所以我对此有点新。在此先感谢您的帮助!
对我而言,这是因为json对象包含一个可选 – onmyway133