0

In the networking between the iPhone and desktop versions of our application, the iPhone sends over the device name for use on the desktop. The problem is that some of the beta testers have tildes (`) in their device names. For some reason when this is in the device name it prevent the socket from sending the actual string data.

I've tried simply cleaning up the device name before sending it, but the tilde in the device name (as entered in iTunes) is not recognized at runtime as a tilde. Here's the code that doesn't work:

NSString *safedevicename = [[UIDevice currentDevice] name];
safedevicename = [safedevicename stringByReplacingOccurrencesOfString:@"`" withString:@"'"];

It finds no occurrences of a tilde, and replaces nothing. I've also used rangeOfString to search for tildes and it returns nothing. I'm 100% sure the character, at least as it's entered in iTunes, is a tilde.

Also, when printing a description of the string to the console, the character is encoded as \u00b4, and when hovering over the variable it appears as a period ..

Anyone know how I can grab this character and get it out of there? Also, isn't there a way in objective C to more easily clean up the string to make sure it's safe to send over a socket?

EDIT: Also something that might be useful, to write the NSString to the NSOutputString I use the following line of code:

len = [oStream write:[[writeString dataUsingEncoding:NSASCIIStringEncoding] bytes] maxLength:[writeString lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];

EDIT #2: This line of code works to replace the Tilde, but I'm sure there are other characters I should be worrying about:

safedevicename = [safedevicename stringByReplacingOccurrencesOfString:@"\\u00b4" withString:@"'"];
mjdth
  • 6,536
  • 6
  • 37
  • 44
  • 2
    First, the ` character is a grave accent, not a tilde. A tilde is ~. Second, I think you are encoding wrong. Whatever character you have in there, B4 cannot be part of the ASCII character encoding. The highest ASCII character is 7F. The tilde is 7E and the grave accent is 60. Sockets are a binary data stream, so anything is 'safe'. Try sending UTF-8 encoded strings instead of ASCII encoded strings. – Jason Coco Mar 25 '10 at 16:34
  • Wow complete brain fart on the name of the character. Guess I need to go back to spanish class. I've tried other characters (ñ, é) and they all cause the same problem. – mjdth Mar 25 '10 at 16:51
  • Also I'll try out NSUTF8Encoding and see how that works out. Thanks for the tip. – mjdth Mar 25 '10 at 16:53
  • Yep that was it. Seems to be working fine now after initial testing. Thanks for the help! – mjdth Mar 25 '10 at 16:59

1 Answers1

0

Jason's comment was the correct answer: I needed to change the encoding from NSASCIIStringEncoding to NSUTF8StringEncoding.

Andy Bourassa
  • 1,838
  • 2
  • 15
  • 17
mjdth
  • 6,536
  • 6
  • 37
  • 44