views:

146

answers:

1

Hi, 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.

+3  A: 

In this case, you might be best off using the NSURLConnection class method +sendSynchronousRequest:returningResponse:error:; it'll return a data object you can initialize your string with (using -initWithData:encoding:). You can specify a timeout by creating an NSURLRequest using its -initWithURL:cachePolicy:timeoutInterval: method, then pass that request as the first parameter of +sendSynchronousRequest:returningResponse:error:.

Noah Witherspoon
Great!!, that was just as I imagined it!! thanks!
sergiobuj