문제

While running iphone app, several times i got the following Error

The connection failed due to a malformed URL

At CFNetwork Error Codes Reference, i got the info that this error triggered by CFNetwork

  • When and Why this error is triggered ?
  • How to get rid from this error?
도움이 되었습니까?

해결책

NSURLConnection has a method canHandleRequest which will return a boolean whether the url is valid or not, e.g.

NSString *strUrl = [NSString stringWithFormat:@"http://test.com/stopBoard/%i",busStopCode];
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
                                              cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                                          timeoutInterval:20.0];
// a cache policy of NO must be used otherwise the previous query is used if the phone is not online

BOOL canGo = [NSURLConnection canHandleRequest:request];
if(canGo){
    connection = [[NSURLConnection alloc] initWithRequest:request 
                                                 delegate:self 
                                         startImmediately:YES];


} else {
    self.errorMessage = @"\n\nurl check failed\n\n";
    return NO;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top