2016-02-27 325 views
3

我想写一些代码来验证我的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) 
      } 
     }) 
    } 
} 

我之前没有做过应用内购买,所以我对此有点新。在此先感谢您的帮助!

+0

对我而言,这是因为json对象包含一个可选 – onmyway133

回答

3

错误消息明确指出,字典中的某个值是NSData对象,JSON不支持该对象。

看来,这是一个代码完成的问题

更换

base64EncodedDataWithOptions([])

base64EncodedStringWithOptions([])

同时也作为链接的文章以书面。


两个旁注:

  • dataWithJSONObject回报NSData无论如何,类型转换是无用的,绝不会是一个隐含的展开可选。
  • 服务器肯定不关心漂亮的印刷JSON。
相关问题