Batista R. Harahap

URLEncode With Objective C - The OAUTH Way

Coming from a PHP background, we should all be thankful to rawurlencode() from PHP! Here's an Objective C version with OAUTH v1.0a requirements.

  
- (NSString *)urlencode:(NSString *)text {
    NSString *temp = @"";

    for(int i=0, max=[text length]; i (char) '.' ||
           t  (char) '_' ||
           t  (char) '-' ||
           t  (char) '~' ||
           t == (char) '#' ||
           (b>=0x30 && b<=0x39) ||
           (b>=0x41 && b<=0x5A) ||
           (b>=0x61 && b<=0x7A)
            ) {
            temp = [temp stringByAppendingFormat:@"%c", t];
        } else {
            temp = [temp stringByAppendingString:@"%"];
            if (b <= 0xf) temp = [temp stringByAppendingString:@"0"];
            temp = [NSString stringWithFormat:@"%@%X", temp, b];
        }
    }

    return temp;
}
16 August 2011 by Batista Harahap on form | http | ios | iphone | objective c | post | url encode | urlencode
comments powered by Disqus