views:

101

answers:

1

I have to POST data to a php page from an Iphone application, so i need to encode the parameters properly, converting the special characters...

Specifically i need to send the UDID of the iphone.

I've found many helps on the web to encode the String to pass as parameter, but i got a strange error.

I'm using this function:

- (NSString *)URLEncodeString:(NSString *)string { 

NSString *result =
   (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
    (CFStringRef)string, NULL, CFSTR("'\"?=&+<>;:-"), kCFStringEncodingUTF8);

return [result autorelease];}

it seems correct but when i use it, the result isn't what i expect for.

This is the code:

UIDevice *device = [UIDevice currentDevice];
NSString *uniqueIdentifier = [device uniqueIdentifier];

NSLog(uniqueIdentifier);
NSLog([self URLEncodeString:uniqueIdentifier]);

and this is the Log generated when i hit this code:

2010-06-23 21:58:51.671 provaNavigation[2343:20b] 00000000-0000-1000-8000-0013775CE6D2
2010-06-23 21:58:51.672 provaNavigation[2343:20b] 0000000010003900009080176010001273086780001283520013775CE6D2

and again:

2010-06-23 21:59:25.614 provaNavigation[2343:20b] 00000000-0000-1000-8000-0013775CE6D2
2010-06-23 21:59:25.615 provaNavigation[2343:20b] 000000001000390000908192801000-18000912258750013775CE6D2

and again:

2010-06-23 21:59:40.848 provaNavigation[2343:20b] 00000000-0000-1000-8000-0013775CE6D2
2010-06-23 21:59:40.849 provaNavigation[2343:20b] 000000001000390000908866081000-18000912239630013775CE6D2

and again...

i get a different value every time and never correct with the right %number.

I'm going crazy, somebody can help me? Thanks

+1  A: 

I've come across problems URL encoding before, so I ended up writing a category on NSString to properly encode URLs:

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

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

There are a couple notes about this:

  1. This will encode spaces as "+"
  2. This encodes everything except a-z (upper and lowercase), numbers, period, hyphen, underscore, and tilde
  3. This will properly encode unicode characters as multiple octets. For example, I can do:

    NSString * enc = [@"http://example.com/?data=÷¡¿" URLEncodedString];
    NSLog(@"%@", enc);
    NSLog(@"%@", [enc URLDecodedString]);

    Which will properly log:

    Testing.m:65 main() http%3A%2F%2Fexample.com%2F%3Fdata%3D%C3%B7%C2%A1%C2%BF
    Testing.m:66 main() http://example.com/?data=÷¡¿

EDIT I copied and pasted it into an empty project, like this:

NSString * string = @"00000000-0000-1000-8000-0013775CE6D2";
NSString *result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("'\"?=&+<>;:-"), kCFStringEncodingUTF8);
NSLog(@"encode? %@", result);
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"decode? %@", result);
NSLog(@"equal? %d", [string isEqual:result]);

And it logs:

Testing[62541:a0f] encode? 00000000%2D0000%2D1000%2D8000%2D0013775CE6D2
Testing[62541:a0f] decode? 00000000-0000-1000-8000-0013775CE6D2
Testing[62541:a0f] equal? 1

The reason your code isn't working is because you're logging the string as the format string, which means it's seeing the %2 stuff and trying to replace it with other things. Observe:

NSString * string = @"00000000-0000-1000-8000-0013775CE6D2";
NSString *result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR("'\"?=&+<>;:-"), kCFStringEncodingUTF8);
NSLog(@"%@", result);
NSLog(result);

Logs:

Testing[62585:a0f] 00000000%2D0000%2D1000%2D8000%2D0013775CE6D2
Testing[62585:a0f] 00000000879923200001897296640100041968968000 00013775CE6D2

So the string is getting properly encoded, but you're just logging it incorrectly.

Dave DeLong
Thanks for your answer.But i'd like to know why the code that i've used works improperly...On the web many people claim that that code works!
iphonePom
@iphonePom edited answer.
Dave DeLong
Damn, i knew that it was something stupid.Thank you very much man!
iphonePom