iOS开发中使app获取本机通讯录的实现代码实例

2020-01-14 19:55:24刘景俊
易采站长站为您分析iOS开发中使app获取本机通讯录的实现代码实例,主要用到了AddressBook.framework和AddressBookUI.framework,代码基于传统的Objective-C,需要的朋友可以参考下  

一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、获取通讯录

1、在infterface中定义数组并在init方法中初始化

复制代码
NSMutableArray *addressBookTemp;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    addressBookTemp = [NSMutableArray array];
}
2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自NSObject的类,在.h中
复制代码
@interface TKAddressBook : NSObject {
    NSInteger sectionNumber;
    NSInteger recordID;
    NSString *name;
    NSString *email;
    NSString *tel;
}
@property NSInteger sectionNumber;
@property NSInteger recordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;
 
@end
在.m文件中进行synthesize
复制代码
@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
 
@end
3、获取联系人

 

在iOS6之后,获取通讯录需要获得权限

复制代码
    //新建一个通讯录类
    ABAddressBookRef addressBooks = nil;
 
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
 
    {
        addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
 
        //获取通讯录权限