ios - NSJSONSerialization Causing Crash in Swift when Verifying App Receipt -
i trying verify app receipt , have done code found online. however, simulating event user not connected internet when attempting validate receipt , getting crash when doing so. following do/catch method causes crash @ anyobj
line when device not connected internet:
func validaterecipt(){ var response: nsurlresponse? var error: nserror?
var receipt: nsdata = nsdata() if let url = nsbundle.mainbundle().appstorereceipturl { if let appreceipt = nsdata(contentsofurl: url) { print("receipt found") receipt = appreceipt }else { print("receipt not found") } }else { print("could not load receipt") } //https://buy.itunes.apple.com/verifyreceipt let request = nsmutableurlrequest(url: nsurl(string: "https://sandbox.itunes.apple.com/verifyreceipt")!, cachepolicy: nsurlrequestcachepolicy.useprotocolcachepolicy, timeoutinterval: 10) let session = nsurlsession.sharedsession() request.httpmethod = "post" let receiptdata:nsstring = receipt.base64encodedstringwithoptions(nsdatabase64encodingoptions.encodingendlinewithlinefeed) //nslog("%@",receiptdata) let payload:nsstring = "{\"receipt-data\" : \"\(receiptdata)\"}" let payloaddata = payload.datausingencoding(nsutf8stringencoding) let err: nserror? request.httpbody = payloaddata var task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in var err: nserror? //let json = nsjsonserialization.jsonobjectwithdata(data!, options: .mutableleaves) as? nsdictionary var json: anyobject? { let anyobj = try nsjsonserialization.jsonobjectwithdata(data!, options: []) as! [string:anyobject] ****//app crashes here ^^//**** json = anyobj } catch { print("fetch failed: \((error nserror).localizeddescription)") } if(err != nil) { print(err!.localizeddescription) let jsonstr = nsstring(data: data!, encoding: nsutf8stringencoding) print("error not parse json: '\(jsonstr)'") } else { if let parsejson = json { print("recipt \(parsejson)") print(parsejson.count) } else { let jsonstr = nsstring(data: data!, encoding: nsutf8stringencoding) print("recipt error: \(jsonstr)") } print("you gets nathan") } }) task.resume() }
the crash fatal error: unexpectedly found nil while unwrapping optional value
i having trouble understanding do/catch method not confident comprehend going on here. i'm guessing question how can avoid crashing when anyobj returns nil?
i guessing line debugger cursor pointing @ is:
let anyobj = try nsjsonserialization.jsonobjectwithdata(data!, options: []) as! [string:anyobject]
...and data
variable nil (i.e., optional contains no value). check before force-unwrapping (!
operator).
(the (error nserror).localizeddescription
can't be, because error supposed non-nil inside catch
block , failing case nserror
give different error message, think).
edit:
this how unwrap safely:
if let safedata = data { // <-- need // data 'nsdata?', safedata 'nsdata'. // if data contained value, assigned safedata. // use safedata source of json (**not** data). } else{ print("error: data nil!") }
Comments
Post a Comment