3

I'm using ABPeoplePickerNavigationController to let the user select an address. Everything works just fine in the Simulator. But if an address contains non-ASCII characters like "ü" the result is strange on my device. I have this code:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

ABMultiValueRef addressesMultiValue = ABRecordCopyValue(person, property);
NSArray *addresses = (__bridge_transfer NSArray*)ABMultiValueCopyArrayOfAllValues(addressesMultiValue);
CFRelease(addressesMultiValue);

NSDictionary *addressData = [addresses objectAtIndex:0];

NSLog(@"%@", addressData);

NSArray *addressKeys = [[NSArray alloc] initWithObjects:(NSString*)kABPersonAddressStreetKey,
                        (NSString*)kABPersonAddressZIPKey,
                        (NSString*)kABPersonAddressCityKey,
                        (NSString*)kABPersonAddressStateKey,
                        (NSString*)kABPersonAddressCountryKey,
                        (NSString*)kABPersonAddressCountryCodeKey, nil];

NSMutableString *address = [[NSMutableString alloc] init];

for (NSString *key in addressKeys) {
    NSString *object = [addressData objectForKey:key];
    if (object) {
        [address appendFormat:@"%@, ", object];
    }
}

NSLog(@"%@", address);

The output for addressData is like this:

{
City = "M\U00fcnchen";
Country = Deutschland;
CountryCode = de;
Street = "Some street";
ZIP = 81000;
}

and the output for address is:

Some street, 81000, München, Deutschland, de, 

The correct output for address would be "Some street, 81000, München, Deutschland, de, ". What puzzles me the most is, that \U00fc is the correct Unicode code point for "ü". I have tried many things including printing out every single unichar on its own, but the result doesn't change. Whatever I do when accessing the value in the NSDictionary seems to kill the Unicode character. What can I do to simply get the address correctly?

Thank you very much in advance!

aus
  • 93
  • 1
  • 9

2 Answers2

3

There is nothing wrong with your code or the output. You are displaying it wrong. The letter 'ü' encoded in UTF-8 is 0xC3 0xBC. In the MacRoman character set the byte 0xC3 represents the character '√' and the byte 0xBC represents 'º'. Look at your output as UTF-8 (which it is) and not as MacRoman (which it is not) and you're set.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • Thank you. I didn't think of that because Xcode's debug window displayed strings from the simulator just fine and the geocoder, to which I forward the address to, failed, too. But after rebooting the device the geocoder worked fine and thus it really was just a display issue! – aus Apr 02 '12 at 16:56
0

Use NSUTF8StringEncoding to encode your string using method: stringWithUTF8String:.

For example,

NSString *str = [NSString stringWithUTF8String:"your string for encoding"]; 

As in your case

NSString *object = [NSString stringWithUTF8String:[[addressData objectForKey:key] cStringUsingEncoding:NSUTF8StringEncoding]];

Or

NSString *object = [NSString stringWithUTF8String:[[addressData objectForKey:key] UTF8String]];

Let me know if you need more help.

Hope this helps.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • How is this supposed to help? You convert a string to an UTF-8 character array and back to a string, this won't change anything at all. – Sven Apr 01 '12 at 15:19
  • @Sven: You can refer to http://stackoverflow.com/questions/5447413/nsstring-unicode-encoding-problem – Parth Bhatt Apr 01 '12 at 15:21
  • Thank you for your answer! But as it turned out it really is just a display issue. – aus Apr 02 '12 at 16:58