如何设置30秒超时? NSURL

时间:2021-02-24 21:12:40

How can I set the 30 sec timeout? NSURL

如何设置30秒超时? 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];

2 个解决方案

#1


4  

From this previous SO question timeout stringwithcontentsofurl

从此前的SO问题超时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:.

在这种情况下,您可能最好使用NSURLConnection类方法+ sendSynchronousRequest:returningResponse:error:;它将返回一个数据对象,您可以使用(使用-initWithData:encoding :)初始化您的字符串。您可以通过使用-initWithURL:cachePolicy:timeoutInterval:方法创建NSURLRequest来指定超时,然后将该请求作为+ sendSynchronousRequest的第一个参数传递:returningResponse:error:。

#2


3  

When you create an NSURLRequest from your NSURL, use requestWithURL:cachePolicy:timeoutInterval: to specify a timeout interval in seconds. For instance:

从NSURL创建NSURLRequest时,请使用requestWithURL:cachePolicy:timeoutInterval:指定超时间隔(以秒为单位)。例如:

NSURLRequest* request = [NSURLRequest requestWithURL: myUrl 
    cachePolicy: NSURLRequestReloadIgnoringCacheData 
    timeoutInterval:30.0];

#1


4  

From this previous SO question timeout stringwithcontentsofurl

从此前的SO问题超时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:.

在这种情况下,您可能最好使用NSURLConnection类方法+ sendSynchronousRequest:returningResponse:error:;它将返回一个数据对象,您可以使用(使用-initWithData:encoding :)初始化您的字符串。您可以通过使用-initWithURL:cachePolicy:timeoutInterval:方法创建NSURLRequest来指定超时,然后将该请求作为+ sendSynchronousRequest的第一个参数传递:returningResponse:error:。

#2


3  

When you create an NSURLRequest from your NSURL, use requestWithURL:cachePolicy:timeoutInterval: to specify a timeout interval in seconds. For instance:

从NSURL创建NSURLRequest时,请使用requestWithURL:cachePolicy:timeoutInterval:指定超时间隔(以秒为单位)。例如:

NSURLRequest* request = [NSURLRequest requestWithURL: myUrl 
    cachePolicy: NSURLRequestReloadIgnoringCacheData 
    timeoutInterval:30.0];