如何以编程方式从 iPhone 地址簿获取地址占位符文本?

发布于 2024-12-02 04:22:53 字数 472 浏览 1 评论 0原文

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

例如

- 美国 占位符是: 街道、城市、邮政编码

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2024-12-09 04:22:53

不确定我是否理解正确。您需要每个字段的区域设置名称 - 例如区域设置翻译,法语、德语等?

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

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

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

也许您喜欢如果我理解错了,请澄清你的问题。

吉米

编辑:

好吧,我仍然不确定我是否正确,但我会回答“两种”方式;)

第一个是您想要通用字段名称(与区域设置无关) ) - 您可以通过这种方式获取这些(在项目的当前语言环境中):代码使用 NSArrays 和 NSDictionaries,具体取决于地址簿卡的条目:

- (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);

    }

}

查看日志,您会看到如何获取您的所有标签和值就像 - 只是延伸将代码添加到您想要的字段中。

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

我不确定是否可以通过编程方式更改此设置。如果您知道一种方法,请告诉我;)

所以,我希望这对您有帮助,喜欢我的正确答案:)

吉米

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文