Неверный URL-адрес iOS для NSUrlConnection

У меня есть URL-адрес в NSString с именем myUrl. Когда я NSLog myUrl я вижу следующее:

http://dl.dropbox.com/u/57665723%2FChocolatesRUs%2FJellies%2FUn-sugared%2Fgen%20no%20sugar.pdf

Затем я пытаюсь подключиться, используя этот URL-адрес, следующим образом:

NSURL* url = [ NSURL URLWithString:nextFileURL ];
NSMutableURLRequest* request =  [ [ NSMutableURLRequest alloc ] initWithURL: url ];
NSURLConnection* conn = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];

Я получаю следующую ошибку:

errorcode=-1000, error=Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x27e540 {NSUnderlyingError=0x26d800 "bad URL", NSLocalizedDescription=bad URL}

я пытался использовать

NSString* myUrl = [myUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

но это тоже не работает.

Кто-нибудь может увидеть проблему?


person whatdoesitallmean    schedule 10.10.2012    source источник


Ответы (2)


Может быть, вы можете избежать своего URL-адреса с помощью stringByAddingPercentEscapesUsingEncoding вместо stringByReplacingPercentEscapesUsingEncoding.

person alexandresoli    schedule 10.10.2012

stringByAddingPercentEscapesUsingEncoding удаляет пробелы и добавляет проценты

NSString *urlString =[NSString stringWithFormat:@"&street=%@&street2=&city=%@&state=%@&
zipcode=%@&candidates=10", address.address2,address.city, address.state, address.zip5];
NSLog(@"BAD URL - %@",urlString ); // there is space in url

NSString *encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSLog(@" CORRECT URL - %@", encodedUrl); // url encode that space by %20


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL RLWithString:encodedUrl]];
person Mohamed Jaleel Nazir    schedule 15.11.2014