iOS URLEncoding

百度找的渣渣都是不能用的= =

所以,用下面的这个Extension就好了

URLEncoding

- (NSString *)URLEncodedString {
    NSMutableString *output = [NSMutableString string];
    const unsigned char *source = (const unsigned char *)[self UTF8String];
    NSInteger sourceLen = strlen((const char *)source);
    for (NSInteger 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;
}

URLDecoding

- (NSString *)URLDecodedString {
    NSString *output = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    output = [output stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return output;
}