objective c - unable to make network call after accessing contacts in iOS? -
i having strange situation here.in app trying access contacts of user's phone when user granted permission trying make network call server.my code works fine in accessing contacts after making network call delegate method never called.
code
abaddressbookref addressbookref = abaddressbookcreatewithoptions(null, null); if (abaddressbookgetauthorizationstatus() == kabauthorizationstatusnotdetermined) { abaddressbookrequestaccesswithcompletion(addressbookref, ^(bool granted, cferrorref error) { // first time access has been granted, add contact nslog(@"access contact"); [self sample];//here function call making network request. }); } else if (abaddressbookgetauthorizationstatus() == kabauthorizationstatusauthorized) { // user has given access, add contact nslog(@"previous access"); } else { // user has denied access // send alert telling user change privacy setting in settings app nslog(@"send alert"); }
function make network request
-(void)sample { nslog(@"sample func called"); // create request. nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://192.168.1.42/login"]]; // specify post request request.httpmethod = @"post"; // how set header fields [request setvalue:@"application/xml; charset=utf-8" forhttpheaderfield:@"content-type"]; // convert data , set request's httpbody property nsstring *stringdata = @"some data"; nsdata *requestbodydata = [stringdata datausingencoding:nsutf8stringencoding]; request.httpbody = requestbodydata; // create url connection , fire request sampleconn= [[nsurlconnection alloc] initwithrequest:request delegate:self]; }
delegate methods
//delegate methods of nsurlconnection - (void)connection:(nsurlconnection*)connection didreceiveresponse:(nsurlresponse *)response { nslog(@"response recieved"); } - (void)connection:(nsurlconnection*)connection didreceivedata:(nsdata*)data { if([connection isequal:sampleconn]) { nslog(@"this sample"); } } - (void)connection:(nsurlconnection*)connection didfailwitherror:(nserror*)error { nslog(@"fail load"); } - (void)connectiondidfinishloading:(nsurlconnection *)connection { }
when call [self sample];
function called delegate method never called & don't response that.but if call function anywhere else in code works fine.
try use
dispatch_async(dispatch_get_main_queue(), ^{ [self sample]; });
Comments
Post a Comment