Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString to NSurl

Tags:

having trouble getting NSSTRING to convert to NSURL, item.image, holds the url for an image that im getting through xml

NSString *urlString = [NSString stringWithFormat:@"%@", item.image]; NSURL *url = [NSURL URLWithString:urlString];  NSLog(@"string> %@ ", urlString); NSLog(@"url> %@ ", url);  2011-06-23 11:18:49.610 Test[10126:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg        2011-06-23 11:18:49.611 Test[10126:207] url> (null)  

also if i try :

NSString *urlString = [NSString stringWithFormat:@"%@", item.image]; NSURL *url = [NSURL fileURLWithPath :urlString];  2011-06-23 11:22:08.063 Test[10199:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg  2011-06-23 11:22:08.064 Test[10199:207] url> %0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://www.harlemfur.com/images/Dog_Olive.jpg%0A%20%20%20%20%20%20%20%20%20%20%20%20 -- /  
like image 310
user616860 Avatar asked Jun 23 '11 15:06

user616860


People also ask

What does NSURL mean?

If you get an error on your iPhone, iPad, or iPod touch that says Cannot play title. Please try again later. ( NSURL:1200;) It usually means a network connectivity issue is preventing the device from reaching the Netflix service. Follow the steps below to fix the problem.

What is NS URL?

An NSURL object is composed of two parts—a potentially nil base URL and a string that is resolved relative to the base URL. An NSURL object is considered absolute if its string part is fully resolved without a base; all other URLs are considered relative.


1 Answers

When making URL from NSString, don't forget to encode it first, so try this:

NSString *urlString = [NSString stringWithFormat:@"%@", item.image]; NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];   

For IOS ≥ 9.0 use

NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; 
like image 50
cxa Avatar answered Sep 17 '22 15:09

cxa