Frage

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?
War es hilfreich?

Lösung

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top