我正在尝试编写一些代码来验证我的iOS应用程序上的订阅。我正在关注本教程:http : //savvyapps.com/blog/how-setup-test-auto-renewable-subscription-ios-app/
它不在Swift 2.0中,因此我不得不转换一些代码,但是我在这一行遇到了麻烦:
let requestData = try! NSJSONSerialization.dataWithJSONObject(receiptDictionary, options: NSJSONWritingOptions.PrettyPrinted) as NSData!
当它到达该行时,它会显示以下错误消息:
由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ JSON写入中的类型无效(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)
}
})
}
}
我之前没有进行过应用内购买,因此对此我有点陌生。在此先感谢您的帮助!
该错误消息明确指出字典中的值之一是NSData
JSON不支持的对象。
看来这是代码完成问题
更换
base64Encoded数据WithOptions([])
与
base64Encoded字符串WithOptions([])
也如链接文章中所写。
两个注意事项:
dataWithJSONObject
NSData
无论如何,如果返回,则类型转换是无用的,并且永远不会是隐式的未包装的可选内容。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句