介绍:

本教程将介绍怎样通过代码从地址簿中获取联系人信息。在iPhone上这实际上比你想象的更容易。你可以提取任何存储于地址簿中的信息,如图片(照片),email地址。注意: iPhone提供了简易的用户联系信息的提取方法但附加了编辑限制。你的程序可以访问所有信息但不允许修改。本教程中,我将介绍提取用户联系信息的简单步骤,最终输出如下所示:

Picture 13 iPhone Tutorial for Retrieving Contact information from AddressBook
图11. 最终输出

开始

现在开始介绍从地址簿提取联系信息的步骤。
步骤1.建立一个新Xcode项目,取名为 ‘RetrieveContactInfo’。

图 1 从地址簿提取联系信息的iPhone教程
图1. 建立Xcode项目
Picture 2 iPhone Tutorial for Retrieving Contact information from AddressBook
图2. 命名你的Xcode项目

步骤 2. 在 ‘Group & File’ 面板打开 ‘Frameworks’。右击任意framework并选择‘Reveal in Finder’ (图 3)。在Finder窗口移到最前面找到‘AddressBook.framework’和 ‘AddressBookUI.framework’。将它们拖入 ‘Frameworks’ 文件夹;不要选取 ‘Copy items into destination group’s folder’。
[5,6,7,8,9,10]

Picture 5 iPhone Tutorial for Retrieving Contact information from AddressBook
图 3. 添加framework
Picture 6 iPhone Tutorial for Retrieving Contact information from AddressBook
图4. 添加framework
Picture 7 iPhone Tutorial for Retrieving Contact information from AddressBook
图 5. 添加framework
Picture 8 iPhone Tutorial for Retrieving Contact information from AddressBook
图 6. 拖入 AddressBook.framekwork 和 AddressBookUI.framework
Picture 9 iPhone Tutorial for Retrieving Contact information from AddressBook
图 7. 不要选取“copy”一项
Picture 10 iPhone Tutorial for Retrieving Contact information from AddressBook
图 8. 加入了framework后

3. 删除 ‘RetrieveContactInfoViewController.h’中代码,用以下代码替代:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
//  RetrieveContactInfoViewController.h
//  RetrieveContactInfo
//
//  Created by Adeem on 26/05/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import
#import
#import
@interface RetrieveContactInfoViewController : UIViewController  {
ABPeoplePickerNavigationController *picker;
IBOutlet UILabel *phoneNo;
IBOutlet UILabel *email;
IBOutlet UILabel *name;
}

-(IBAction)chooseContacts;

@end

4. 在 ‘RetrieveContactInfoViewController.m’中 @end前,加入下列代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-(IBAction)chooseContacts {
    // creating the picker
    if(!picker){
        picker = [[ABPeoplePickerNavigationController alloc] init];
        // place the delegate of the picker to the controll
        picker.peoplePickerDelegate = self;
    }

    // showing the picker
    [self presentModalViewController:picker animated:YES];
}

/**************  Contacts Delegate Functions  ***************/

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
        shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSMutableArray *phones = [[NSMutableArray alloc] init];
    int i;
    for (i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
        NSString *aPhone = [(NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i) autorelease];
        // NSLog(@"PhoneLabel : %@ & Phone# : %@",aLabel,aPhone);
        [phones addObject:aPhone];
    }

    NSString *mobileNo = [phones objectAtIndex:0];
    phoneNo.text = mobileNo;
    NSLog(mobileNo);
    name.text = (NSString*)ABRecordCopyCompositeName(person);

    ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
    NSMutableArray *emails = [[NSMutableArray alloc] init];
    for (i = 0; i  0)
    {
        NSString *emailAdress = [emails objectAtIndex:0];

        email.text = emailAdress;
        NSLog(emailAdress);
    }
    [peoplePicker dismissModalViewControllerAnimated:YES];

    return YES;
}

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

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    // assigning control back to the main controller
    [picker dismissModalViewControllerAnimated:YES];
}

5. 打开 ‘RetrieveContactInfoViewController.xib’文件。在视图中加入三个标签和一个按钮。

Picture 11 iPhone Tutorial for Retrieving Contact information from AddressBook
图 9. 视图的模板设计

6. 在xib文件中选择‘Files Owner’并按cmd + 2打开‘Connection Inspector’。在Outlets中,选择email边上的圆圈并将其拖到第二个标签。用同样方法映射其他标签到控制器。

Picture 12 iPhone Tutorial for Retrieving Contact information from AddressBook
图 10. 映射类变量到Interface Builder

7. 运行程序,最终结果如图:

Picture 13 iPhone Tutorial for Retrieving Contact information from AddressBook
图11. 最终结果

代码 下载

原文见:iPhone Tutorial for Retrieving Contact information from AddressBook