40

I have following code in my application.

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];

pathOfThumbNail has following path


http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg


When I open above path in safari browser - path is changed automatically & image is successfully displayed.

http://70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg


But in iPhone, due to space in path, image isn't loaded in nsdata.

sagarkothari
  • 24,520
  • 50
  • 165
  • 235

3 Answers3

101

Use: stringByAddingPercentEscapesUsingEncoding:

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character

Added per request by @rule

NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(@"urlText:        '%@'", urlText);
NSLog(@"urlTextEscaped: '%@'", urlTextEscaped);
NSLog(@"url:            '%@'", url);

NSLog output:

urlText:        '70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg'  
urlTextEscaped: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'  
url:            '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'  
zaph
  • 111,848
  • 21
  • 189
  • 228
  • 2
    Great ! Exactly as I wanted. Actually I knew it but forgotten due to project load. Hats of for your kind help to me. – sagarkothari Sep 17 '09 at 20:44
  • 1
    Your answer is pretty good, but you should give an example like: NSString* urlText = @"http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg"; urlText = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString: urlText]; – rule Aug 31 '12 at 07:46
  • Note: stringByAddingPercentEscapesUsingEncoding doesn’t encode reserved characters like ampersand (&) and slash (/) – Deeper Jan 21 '15 at 01:27
16

A swift 3.0 approach (stringByAddingPercentEscapesUsingEncoding and stringByAddingPercentEncodingWithAllowedCharacters seems now deprecated):

let urlString ="your/url/".addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
Niko Zarzani
  • 1,372
  • 2
  • 15
  • 28
5

stringByAddingPercentEscapesUsingEncoding has been deprecated in iOS 9.0, it is recommended you use stringByAddingPercentEncodingWithAllowedCharacters instead.

Here's the Objective-C code for > iOS 9.0

NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString: urlTextEscaped];

NSLog(@"urlText:        '%@'", urlText);
NSLog(@"urlTextEscaped: '%@'", urlTextEscaped);
NSLog(@"url:            '%@'", url);
Priest
  • 242
  • 4
  • 11