Reputation: 11841
I am creating a URL string like so:
[Items appendString:[object objectForKey:@"Items"]];
[Items appendString:@"*"];
[Items deleteCharactersInRange:NSMakeRange([Items length]-1, 1)];
//This returns this: ~SEWER/FLATWORK SUPPLY & INSTALL - 25% of CONTRACT*~SEWER/FLATWORK SUPPLY & INSTALL - 75% of CONTRACT*SUMP PUMP PIT
//add Items to URL
NSString *fullURL = [NSString stringWithFormat:@"https://example.com?Items=%@, [Items stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
but it returns like so:
Items=~SEWER/FLATWORK%20SUPPLY%20&%20INSTALL%20-%2025%25%20of%20CONTRACT*~SEWER/FLATWORK%20SUPPLY%20&%20INSTALL%20-%2075%25%20of%20CONTRACT*SUMP%20PUMP%20PIT
how do I get it return like this:
%20%26%20
instead of %20&%20
for the &
?
Upvotes: 2
Views: 195
Reputation: 86651
I think the issue is that the method tries to be too clever - it only does as much as is necessary to get a legal URL and because you don't have a question mark in your string, it probably thinks it is OK to leave the ampersands in.
Try constructing the whole URL and do the escaping on the whole URL.
NSString *fullURL = [[@"https://example.com?Items=" stringByAppendingString: items]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Or perhaps use stringByAddingPercentEncodingWithAllowedCharacters:.
Upvotes: 2
Reputation: 181
Use CFURLCreateStringByAddingPercentEscapes() for getting UTF8stringencoding of characters
NSString *urlString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef) Items, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8))
Upvotes: 0
Reputation: 27598
Try this.
fullURL=[fullURL stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
NSLog(@"fullURL: %@ ...", fullURL);
Upvotes: 0