I try to retrive data from certain url with command:
-(NSMutableData *) callUrl: (NSString *)url withData:(NSMutableDictionary *)data delegate:(id) delegate {
NSURL *executeUrl = [NSURL URLWithString:<string>];
NSURLRequest *request = [NSURLRequest requestWithURL: executeUrl
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSMutableData *receivedData = nil;
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:delegate];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
@throw @"Connection error";
}
return receivedData;
}
In delegate (both after connectionDidFinish and connectionDidFailWithError) I do:
//some uninvasive alerts
// release the connection, and the data object
[connection release];
[receivedData release];
Problem is when I provide bad url I got proper error - it's good part - but then I want to execute second url - good for sure, I've got 1003 error - NSURLErrorCannotFindHost.
After around 1-2 min I'm succesfully call url and get data. I suspect some timeouts and ports business, but changing timeout in NSURLRequest doesn't change a thing.
UPDATE
As it turned out - Administrators had some issues with DNS server reached through WiFi network. Code is fine. Thanks for response. If some has similiar problems: try ip address instead of hostname.
From Apple iOS developer documentation, 1003 error refers to when the host name for a URL cannot be resolved. To avoid DNS failures in wifi, overloaded DNS scenarios, it is preferable to resolve ip from hostname for subsequent use or to hardcode the ip address directly, if you do not intend to shift the hosting later on.
Apple documentation:
These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”.
before making any new connection call cancel previous connection. using
I did 2 things to fix this issue :
I used this before initiating my NSUrlConnection
[NSURLConnection cancelPreviousPerformRequestsWithTarget:self];
Don't know which one fixed it but the issue was resolved.