xcode - How to check a timeout occurred in ios url connection? -
i have used urlrequest fetch html file pc in wifi network. app fetches html file fileserver , filename number typed in app. have given 20 seconds timeout request. how can detect whether timeout occurred because have 2 situations, 1. file not exist 2. connection slow
in urlrequest, there bool error,no description. suggest me method if possible. code below urlrequest
-(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 not exist." message:@"try again." delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil]; self.view.userinteractionenabled = yes; [alrt show]; }); } }]; [task resume]; }
since can not comment writing answer.
you need check error object see type of error occured.
so in else block need check error.localizeddescription
see has happened, tell file not found or if request timed out. can use alert show changing else block follows
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]; }); }
Comments
Post a Comment