Unsupported iOS URL

I have a valid url and am getting an invalid url error. Can someone tell me why?

As you can see, there is http: //

// http://fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065 Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x78f97920 {NSUnderlyingError=0x79f78bd0 "unsupported URL", NSLocalizedDescription=unsupported URL} 

This is how I tried to initialize the URL:

Method 1:

 NSString * path=@ "http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065"; NSURL *url=[NSURL URLWithString:path]; NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:url]; 

Method 2:

 NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065"]]; 
+7
url ios objective-c xcode
source share
1 answer

The URL cannot contain characters that are not in the ASCII character set; such characters must be escaped.

Use stringByAddingPercentEncodingWithAllowedCharacters with URLQueryAllowedCharacterSet Character URLQueryAllowedCharacterSet

 NSString *path = @"http://fr.radiovaticana.va/news/2015/02/01/le_pape_françois_à_sarajevo_le_6_juin_prochain/1121065"; NSString *escapedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSLog(@"escapedPath: %@", escapedPath); 

Output:

  escapedPath: http: //fr.radiovaticana.va/news/2015/02/01/le_pape_fran%C3%A7ois_%C3%A0_sarajevo_le_6_juin_prochain/1121065 \ 

See character set for encoding URL Documentation

+29
source share

All Articles