Skip to content

Instantly share code, notes, and snippets.

@Duraiamuthan
Created April 21, 2014 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Duraiamuthan/11147252 to your computer and use it in GitHub Desktop.
Save Duraiamuthan/11147252 to your computer and use it in GitHub Desktop.
Objective C UrlEncode Category
//header
@interface NSString (UrlEncode)
- (NSString *)urlencode;
@end
//implementation
#import "NSString+UrlEncode.h"
@implementation NSString (UrlEncode)
- (NSString *)urlencode {
NSMutableString *output = [NSMutableString string];
const unsigned char *source = (const unsigned char *)[self UTF8String];
int sourceLen = strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
const unsigned char thisChar = source[i];
if (thisChar == ' '){
[output appendString:@"+"];
} else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
(thisChar >= 'a' && thisChar <= 'z') ||
(thisChar >= 'A' && thisChar <= 'Z') ||
(thisChar >= '0' && thisChar <= '9')) {
[output appendFormat:@"%c", thisChar];
} else {
[output appendFormat:@"%%%02X", thisChar];
}
}
return output;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment