objective c - IOS 'NSInvalidArgumentException', reason: 'data parameter is nil' -


the below code im using post data server,it working fine.but app going crash , im getting error "'nsinvalidargumentexception', reason: 'data parameter nil'".i checked internet connection internet connection fine no doubt connection.how solve issue.

nsstring *url = [nsstring stringwithformat:@"http://myurl.com/test"];  nsstring *username   = [_emailloginstring  stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];  nsstring *password = [_passwordloginstring  stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];  nsmutablestring* requesturl = [[nsmutablestring alloc] initwithstring:url];  nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl: [nsurl urlwithstring: [nsstring stringwithstring:requesturl]]]; [request sethttpmethod: @"post"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:[[nsstring stringwithformat:@"username=%@&password=%@",  username,password] datausingencoding:nsutf8stringencoding]];  nsurlresponse *response; nserror *err;  nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request  returningresponse:&response error:&err];  nsstring *serverrplyloginstring = [[nsstring alloc] initwithdata:responsedata encoding:nsasciistringencoding];  nsdictionary *dictobj=[nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&err];   _serverrplyloginstring=[dictobj objectforkey:@"error"];  nslog(@"login response :%@",_serverrplyloginstring); 

first of all, recommend have exception breakpoint enabled:

enter image description here

enter image description here

with exception breakpoints easier fix crash since app pause @ exact line caused exception. now, let's crash:

your app crashes because, message says, data parameter nil. where? here:

nsdictionary *dictobj=[nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&err]; 

while can create nsstring data parameter of nil:

nsstring *serverrplyloginstring = [[nsstring alloc] initwithdata:responsedata encoding:nsasciistringencoding]; 

you cannot create nsdictionary using json serialization nil data parameter. recommend check if response nil , handle situation appropriately.

oh, , 1 last thing. advise use async requests on synced because latter going freeze main queue (i.e. make app stutter). make simple async request this:

[nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) {     if (error || data == nil) {         // handle error     } else {         // process response data     } }]; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -