1
NSMutableString *string = [[NSMutableString alloc]initWithString: @"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%26image%3Duploads%2Fdeals%2FdealImage%2Fdeal_1383005121_IGA+Logo.JPG"]; [string replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])]; NSURL * url = [NSURL URLWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
6
  • working for other urls Commented Oct 29, 2013 at 10:03
  • but not working specially with the above mentioned url Commented Oct 29, 2013 at 10:05
  • i am testing this on iOS 7 Commented Oct 29, 2013 at 10:06
  • Change http%3A%2F%2F to http:// Commented Oct 29, 2013 at 10:20
  • why are you using an already percentage escaped string and you percentage escape again? Use the normal one, manipulate the string if you need (replacing "+" wiht " ") and then escape it. Commented Oct 29, 2013 at 10:25

3 Answers 3

1

one of my colleague faced the same problem , try below line of code . Hope your problem will be resolved

NSMutableString *string = [[NSMutableString alloc]initWithString: @"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%26image%3Duploads%2Fdeals%2FdealImage%2Fdeal_1383005121_IGA+Logo.JPG"]; string = [[NSMutableString alloc] initWithString:[[string stringByURLDecode] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; NSURL * url = [NSURL URLWithString:string]; NSLog(@"url = %@",url); 

Where stringByURLDecode is a category method used to decode URL and it goes like this

- (NSString *)stringByURLDecode { NSMutableString *tempStr = [NSMutableString stringWithString:self]; [tempStr replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])]; return [[NSString stringWithFormat:@"%@",tempStr] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; } 

Best of Luck (y)

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, this doesn't yield back a correctly encoded URL: it is: 'http://support24hour.com/workplace2/buttler/image.php?width=534&height=256&image=uploads/deals/dealImage/deal_1383005121_IGA%20Logo.JPG', but it should be: 'http://support24hour.com/workplace2/buttler/image.php?width=534&height=256&image=uploads/deals/dealImage/deal_1383005121_IGA+Logo.JPG'. Notice the "+" which replaces spaces in a url-encoded parameter string, as defined in the algorithm defined in the w3c spec.
1

You must not percent encode the whole string. Instead, only the data octet in a component must be possibly encoded.

In other words, a URI consists of components and subcomponents which are delimited by certain characters. These characters are called "reserved characters". The separators for the different components must certainly not be included in the percent encoding.

Note that each component of a URL may require a slightly different percent encoding.

See also RFC 3986.


Edit:

The general structure of a URL is given below:

 URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 

The following two examples are straight copied from RFC which show their component parts:

 foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment | _____________________|__ / \ / \ urn:example:animal:ferret:nose 

The "query component" consists of a list of "key/value" parameters whose key and value is separated by "=", and whose parameters are separated by "&".

The query component is everything after the question mark '?' up until a hash character '#'.

The following code encodes the name or the value of a parameter. Note that parameters must be separated by a '&', and the name and value must be separated by a '='.

static NSString* form_urlencode_rfc3986(NSString* s) { CFStringRef charactersToLeaveUnescaped = CFSTR(" "); //CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@~"); // Modified for urls (excluding '~'): CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@"); NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (__bridge CFStringRef)s, charactersToLeaveUnescaped, legalURLCharactersToBeEscaped, kCFStringEncodingUTF8)); return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; } 

6 Comments

Yes. It should be like @"http://support24hour.com/workplace .... The query component can be encoded as will be shown shortly in the Edit, wait a second.
if i change 5121_IGA+Logo.JPG to 5121_IGALogo.JPG
i just removed the '+' character above
Note: unless the "path" and "authority" component consists of unusual characters which are reserved characters (which is very, very, unlikely) then you only need to percent encode the value and key of the query component. Once, you separately encoded them, compose a "parameter-string" with a format string like @"%@=%@". Then compose the parameters to the "query-string" separating them with a "&". Then. finally add this (encoded) query string to the url.
i am experimenting my stuff with your answer...will let you know ASAP..thanks
|
0

Hmmm... how about printing out string before you turn it into an NSURL? Does it look as you'd expect it?

Also: replacing "+" with a Space OR do you actually want to remove it?

3 Comments

printing is same as expected
second line is for decoding
ok, but wouldn't "replace + with [Space]" make... deal_1383005121_IGA[Space]Logo.JPG out of deal_1383005121_IGA+Logo.JPG ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.