How can I set the 30 sec timeout? NSURL
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q开发者_如何学运维=%@&output=csv", [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
From this previous SO question timeout stringwithcontentsofurl
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:
.
When you create an NSURLRequest
from your NSURL
, use requestWithURL:cachePolicy:timeoutInterval:
to specify a timeout interval in seconds. For instance:
NSURLRequest* request = [NSURLRequest requestWithURL: myUrl
cachePolicy: NSURLRequestReloadIgnoringCacheData
timeoutInterval:30.0];
精彩评论