如何以编程方式从iphone地址簿获取地址占位符文本? [英] How to programmatically get address placeholder text from iphone addressbook?

查看:167
本文介绍了如何以编程方式从iphone地址簿获取地址占位符文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为用户提供一种在基于位置的应用程序中输入地址的方法,我希望它看起来与iPhone联系人/地址簿中的地址完全相同。这意味着我需要根据所选国家/地区更新每个字段的占位符文本。

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.

例如 -

美国占位符为:
街道,城市, ZIP

英国占位符为:
街道,城市,邮政编码

我还必须确保占位符映射到代码中的正确字段(即县映射到州,邮政编码映射到zip)。我目前的方法非常繁琐:通过并选择联系人中的每个国家/地区并手动复制文本,然后确保字段正确映射。

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.

是否有任何程序化这样做的方法?即使是通过私有API调用,我只需要执行一次以获取信息,然后我可以删除代码。我在Apple的文档中找不到提供此类信息的任何内容(ABAddressBook文档)。

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?

一般来说,地址字段不多 - 请参阅:ABPerson.h头文件:

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;

如果您需要字段名称(因为可能有个人幻想名称;)),请确保像这样使用ABAddressBookCopyLocalizedLabel:

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

编辑:

好的,我仍然不确定我是否帮助你,但我会回答两种我得到你的方式;)

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

首先是你想要通用字段名称(区域设置独立) - 您可以通过以下方式获取(在您项目的当前区域设置中):代码使用NSArrays和NSDictionaries,具体取决于地址簿卡的条目:

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.

我的答案的另一部分:我想知道你是否只是想看到用户可能拥有的不同语言的标签作为区域设置。如法语,德语等。如果你想看到这个(让ABAddressBookCopyLocalizedLabel使用不同的语言)我只在项目的.plist文件中找到了本地化本机开发区域。如果更改此设置,则会更改翻译。以用户语言显示标签。

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 :)

吉米

这篇关于如何以编程方式从iphone地址簿获取地址占位符文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆