如何检查ios url连接中发生的超时?
我使用URLRequest从wifi网络中的pc获取html文件。 我的应用程序从我的文件服务器获取一个html文件,文件名是在应用程序中键入的数字。我还为请求提供了20秒的超时时间。我如何检测是否发生超时,因为我有两种情况, 1.文件不存在 2.连接很慢 在urlrequest中,有一个BOOL表示错误,没有描述。 如果可能的话,给我一个建议。 下面是我的代码,仅供参考如何检查ios url连接中发生的超时?,ios,xcode,timeout,detect,urlrequest,Ios,Xcode,Timeout,Detect,Urlrequest,我使用URLRequest从wifi网络中的pc获取html文件。 我的应用程序从我的文件服务器获取一个html文件,文件名是在应用程序中键入的数字。我还为请求提供了20秒的超时时间。我如何检测是否发生超时,因为我有两种情况, 1.文件不存在 2.连接很慢 在urlrequest中,有一个BOOL表示错误,没有描述。 如果可能的话,给我一个建议。 下面是我的代码,仅供参考 -(void)getHtmlContent{ [self.spinner startAnimating]; NSString
-(void)getHtmlContent{
[self.spinner startAnimating];
NSString *str = @"http://192.168.1.250/rec/";
// NSString *str = @"file:///Volumes/xampp/htdocs/";
str = [str stringByAppendingString:numEntered.text];
str = [str stringByAppendingString:@".html"];
NSURL *url=[NSURL URLWithString:str];
//self.htmlContent = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
request.timeoutInterval = 20.0;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
if(!error){
if([request.URL isEqual:url]){
NSString *content = [NSString stringWithContentsOfURL:localfile encoding: NSUTF8StringEncoding error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[numEntered setText:@"0"];
text = @"";
[self.spinner stopAnimating];
self.htmlContent = content;
NSString *myHTMLString = self.htmlContent;
if(myHTMLString != nil) {
if([myHTMLString isEqualToString:@"3"]){
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"LogIn Success" message:@"" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
self.view.userInteractionEnabled = YES;
[alrt show];
}
else{
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"LogIn Failed. Try again." message:@"User not authenticated" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
self.view.userInteractionEnabled = YES;
[alrt show];
}
}
});
}
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.spinner stopAnimating];
[numEntered setText:@"0"];
text = @"";
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Requested file does not exist." message:@"Try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
self.view.userInteractionEnabled = YES;
[alrt show];
});
}
}];
[task resume];
}
您必须使用此委托方法来处理超时:-
-(void) connection:(NSURLConnection * ) connection didFailWithError:(NSError *)error {
if (error.code == NSURLErrorTimedOut)
// handle error as you want
NSLog(@"Request time out");
}
您必须使用此委托方法来处理超时:-
-(void) connection:(NSURLConnection * ) connection didFailWithError:(NSError *)error {
if (error.code == NSURLErrorTimedOut)
// handle error as you want
NSLog(@"Request time out");
}
因为我不能发表评论,所以我写这封信作为答复 您需要检查错误对象以查看发生的错误类型 因此,在else块中,您需要检查
error.localizedDescription
以查看发生了什么,它通常会告诉您未找到文件或请求是否超时。您甚至可以使用警报来显示它,就像更改else块一样,如下所示
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.spinner stopAnimating];
[numEntered setText:@"0"];
text = @"";
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle : @"Error"
message : error.localizedDescription
delegate : self
cancelButtonTitle : @"Ok"
otherButtonTitles : nil];
self.view.userInteractionEnabled = YES;
[alrt show];
});
}
因为我不能发表评论,所以我写这封信作为答复 您需要检查错误对象以查看发生的错误类型 因此,在else块中,您需要检查
error.localizedDescription
以查看发生了什么,它通常会告诉您未找到文件或请求是否超时。您甚至可以使用警报来显示它,就像更改else块一样,如下所示
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.spinner stopAnimating];
[numEntered setText:@"0"];
text = @"";
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle : @"Error"
message : error.localizedDescription
delegate : self
cancelButtonTitle : @"Ok"
otherButtonTitles : nil];
self.view.userInteractionEnabled = YES;
[alrt show];
});
}
请查看错误描述并查看此内容。请查看错误描述并查看此内容。谢谢。error.localizedDescription对我来说足以解决这个问题谢谢。error.localizedDescription对我来说足以解决这个问题