check if sendsynchronousrequest was successful
i just wanted to know if this is the right way to check if a send开发者_运维问答synchronousrequest was successful:
NSData* returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
if(returnData == nil)
{
//code showing an alertView for example
}
else
{
//do somthing else
}
thanks in advance for your feedback
seanI think you want to pass something in for your error:
NSData* returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&error];
if(error != 0)
// good to go
else
// error
But I could be wrong, just quickly glanced at it.
I did see this post though.. I think he's asking a similar thing:
http://discussions.apple.com/thread.jspa?threadID=1647649
If you're working with HTTP, then you might also check the status code. For example:
NSHTTPURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if ((nil != data) && (200 == [response statusCode])) {
// Process data
}
The correct way is to define an error variable set it nil and use its reference in nsurlconnection such that the error is returned in that reference memory position .Check the memory is nil and if nil success,if it has some valid memory it is error
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (error ==nil ) {
// Success mechanism
}
else
{
//Failure Mechanism
}
精彩评论