从配置文件页面(电话号码,电子邮件地址,照片)中提取联系人变量到iOS电话簿联系人列表

我想将应用程序中用户 Profiles 的信息加载到iPhone电话簿/联系人列表中 .

我的应用程序的用户配置文件包含有关他们所代表的人的各种属性,例如他们的电话号码,姓名,学校,教育程度,电子邮件地址,照片,他们的工作摘要,他们的兴趣等 .

我希望能够做到的是,另一个用户能够从该配置文件页面中提取(只需单击一下)联系人属性,以便将其导入到iPhone联系人列表中 .

例如,如果我喜欢UserA,并且我想将她添加到我的iPhone联系人列表中,我可以点击“添加到联系人”,然后导入所有UserA的相关 Profiles 信息(电话号码,电子邮件地址,街道)地址,网址,照片等)并在我的iPhone电话簿中创建UserA作为新联系人 .

这可能使用iphone的abpeoplepicker api吗?如果是这样,我该如何执行此操作(我在哪里可以参考正确的文档),以及可能的限制/约束/标准是什么?

回答(1)

2 years ago

框架:

#import "AddressBook/AddressBook.h"

码:

- (void) getLocalContacts
{
    ABAddressBookRef addressBook = ABAddressBookCreate( );
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    User *user;
    NSMutableArray *allContacts = [[NSMutableArray alloc] init];

    for ( int i = 0; i < nPeople; i++ )
    {
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);

        if(ABMultiValueGetCount(emails) != 0)
        {
            user = [[User alloc] init];

            CFStringRef fName, lName;
            fName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
            lName  = ABRecordCopyValue(person, kABPersonLastNameProperty);

            CFStringRef email = ABMultiValueCopyValueAtIndex(emails, 0);


            NSData  *imgData = (NSData *)ABPersonCopyImageData(person);



            NSString *firstName = (NSString *) fName;
            NSString *lastName = (NSString *) lName;


            if (firstName.length == 0 && lastName.length != 0){
                user.userName = lastName;
            } 
            else if (firstName.length != 0 && lastName.length == 0){
                user.userName = firstName;
            }
            else if (firstName.length == 0 && lastName.length == 0){
                user.userName = @"";
            }
            else if (firstName.length != 0 && lastName.length != 0){
                user.userName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            }


            //user.firstName = (NSString *) firstName;
            user.lastName = @"";

            user.email = (NSString *) email;

            user.firstName = (NSString *) email;

            user.localImage = [UIImage imageWithData:imgData];

            [allContacts addObject:user];

            [user release];

        }
    }

    [DataManager sharedManager].allLocalUsers = allContacts;

    [self hideSpinner];

}