Tilde in device name causing problems with NSOutputStream socket
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:@"'"];
Jason's comment was the correct answer: I needed to change the encoding from NSASCIIStringEncoding to NSUTF8StringEncoding.