Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timeout stringwithcontentsofurl

Tags:

nsstring

I have this call to stringwithcontentsofurl:

[NSString stringWithContentsOfURL:url usedEncoding:nil  error:nil];

How can I give that a simple timeout? I don't want to use threads or operation queues (the content of the url is about 100 characters), I just don't want to wait too long when the connection is slow.

like image 211
sergiobuj Avatar asked Jun 12 '10 00:06

sergiobuj


1 Answers

Here's my take on it:

NSString* link = @"http://example.com";
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:0 timeoutInterval:5];
NSURLResponse* response=nil;
NSError* error=nil;
NSData* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* stringFromServer = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
like image 127
Chris Avatar answered Oct 20 '22 22:10

Chris