Quantcast
Channel: iOS Developers » TCP/IP
Viewing all articles
Browse latest Browse all 7

Can we connect

$
0
0

Get A Small File Method

This test will lock up if there is WiFi but no internet connection:


if (![self.reachability isReachable])

This works much better as it tests the connection to an actual file on the server you want to connect to (assuming you can get a named file from it) and allows you to specify a timeout:


    //----- TRY AND DOWNLOAD FILE WITH TIMEOUT -----
	//This avoids lockup if we have WiFi connectivity but can't reach the server for any reason
	NSString *urlString = @"http://www.mydomain.com/some_file.txt";
	NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3.0];		//Set timeout
	NSURLResponse* response = nil;
	NSError* error = nil;
	NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	NSString* stringFromServer = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //autorelease];
	//stringFromServer will be @"" if file could not be retrieved due to no server response, no internet connectivity or if WiFi and GSM are not connected.

 

 


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles



Latest Images