Created
April 21, 2014 16:08
-
-
Save Duraiamuthan/11147252 to your computer and use it in GitHub Desktop.
Objective C UrlEncode Category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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