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.
In this case, you might be best off using the
NSURLConnectionclass 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 anNSURLRequestusing its-initWithURL:cachePolicy:timeoutInterval:method, then pass that request as the first parameter of+sendSynchronousRequest:returningResponse:error:.