开发者

How to programmatically get address placeholder text from iphone addressbook?

I am trying to present a way for the user to enter addresses in a location based application, and I want it to look exactly like the address from the iPhone Contacts/Address Book. This means that I need the placeholder text for each field to be updated according to the country selected.

For example-

United States placeholders are: Street, City, State, ZIP

United Kingdom placeholders are: Street, City, County, Post Code

I also have to make sure that the placeholders map to the correct field in code (i.e. that "County" maps to state and that "Post开发者_如何学编程 Code" maps to zip). My current approach is very cumbersome: go through and select each country in a contact and copy the text by hand, then make sure the fields map correctly.

Is there ANY programmatic way to do this? Even if it is through a private API call, I only need to do it once to get the info, then I can remove the code. I can't find anything in Apple's documentation to provide this type of information (ABAddressBook documentation).


Not sure, if I get you right. You need the locale name of each field - e.g. the locale translation, in french, german, etc?

In general there are not to much fields for addresses - see: ABPerson.h header file:

// Addresses
extern const ABPropertyID kABPersonAddressProperty;            // Street address - kABMultiDictionaryPropertyType
extern const CFStringRef kABPersonAddressStreetKey;
extern const CFStringRef kABPersonAddressCityKey;
extern const CFStringRef kABPersonAddressStateKey;
extern const CFStringRef kABPersonAddressZIPKey;
extern const CFStringRef kABPersonAddressCountryKey;
extern const CFStringRef kABPersonAddressCountryCodeKey;

And if you need the field name (cause there could be personal fantasy names ;) ), be sure to use ABAddressBookCopyLocalizedLabel like this:

CFStringRef label = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multiValue, j));

Maybe you like to clarify your question, if I got you wrong.

jimmy

EDIT:

Ok, I'm still not sure if I got you right, but I will answer 'both' ways I got you ;)

The first is that you want the generic field names (locale independent) - you can get those (in the current locale of you project) that way: The code is using NSArrays and NSDictionaries depending of the entry of the address book card:

- (void) logAddressBook {

    ABAddressBookRef addressBook = ABAddressBookCreate();

    NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

    int i;
    for(i = 0; i < [addresses count]; i++) {
        ABRecordRef record = [addresses objectAtIndex:i];
        NSString *firstName = (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
        NSString *lastName = (NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);

        NSLog(@"%@, %@", lastName, firstName);

        ABMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonEmailProperty);

        int count = ABMultiValueGetCount(multiValue);
        int j;
        for(j = 0; j < count; j++) {
            CFStringRef label = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multiValue, j));
            NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, j);

            NSLog(@"Email for %@: %@", label, value);

            CFRelease(label);
        }

        //Get the contact´s addresses
        CFTypeRef adressesReference = ABRecordCopyValue((ABRecordRef)record, kABPersonAddressProperty);    

        CFIndex mvCount = ABMultiValueGetCount(adressesReference);
        if (mvCount > 0) {
            NSLog(@"Addresses: ");    
            for (j=0; j < mvCount; j++) {
                CFStringRef key = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(adressesReference, j));
                NSDictionary *values = (NSDictionary *)ABMultiValueCopyValueAtIndex(adressesReference, j);

                NSLog(@"%@ - ", key);
                NSEnumerator *enumerator = [values keyEnumerator];
                id innerKey;

                while ((innerKey = [enumerator nextObject])) {
                    /* code that uses the returned key */
                    NSString *value = (NSString *)[values objectForKey: innerKey];
                    CFStringRef innerKeyLabel = ABAddressBookCopyLocalizedLabel((CFStringRef)innerKey);
                    NSLog(@"key: %@ -> value: %@", innerKeyLabel, value);
                }    

            }             
        }

        CFRelease(adressesReference);

    }

}

Look at the log and you see how to get all labels and values you like - just extend the code to the fields you want.

The other part of my answer: I wonder if you just wanted to see the labels in the different languages a user might have as locale. Such as french, german, etc. If you want to see this (having ABAddressBookCopyLocalizedLabel to use different languages) I only found 'Localization native development region' in the .plist file of the project. If you change this, the translation is changed. Showing you the labels in the users language.

I'm not sure if it is possible to change this programmatically. If you know a way, let me know ;)

So, I hope this helped an you like my corrected answer :)

Jimmy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜