码迷,mamicode.com
首页 > 移动开发 > 详细

iOS通过app读取通讯录信息(整理)

时间:2014-05-23 02:03:26      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:ios   通讯录   addressbook   addressbookui   abaddressbookref   

iOS通过app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和AddressBook两个包,并且引入头文件
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
具体实现如下:

-(void)readAllPeoples

{
    //定义通讯录名字为addressbook
    ABAddressBookRef tmpAddressBook = nil;
    
    //根据系统版本不同,调用不同方法获取通讯录
    if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
        tmpAddressBook=ABAddressBookCreateWithOptions(NULL, NULL);
        dispatch_semaphore_t sema=dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(tmpAddressBook, ^(bool greanted, CFErrorRef error){
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        dispatch_release(sema);
    }
    else
    {
        tmpAddressBook =ABAddressBookCreate();
    }
    //取得通讯录失败
    if (tmpAddressBook==nil) {
        return ;
    };
    
    //将通讯录中的信息用数组方式读出
    NSArray* tmpPeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
    
    //遍历通讯录中的联系人
    for(id tmpPerson in tmpPeoples){
        
        //获取的联系人单一属性:First name
        NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);
        NSLog(@"First name:%@", tmpFirstName);
        [tmpFirstName release];
        
        //获取的联系人单一属性:Last name
        NSString* tmpLastName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);
        NSLog(@"Last name:%@", tmpLastName);
        [tmpLastName release];
        
        //获取的联系人单一属性:Nickname
        NSString* tmpNickname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNicknameProperty);
        NSLog(@"Nickname:%@", tmpNickname);
        [tmpNickname release];
        
        //获取的联系人单一属性:Company name
        NSString* tmpCompanyname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonOrganizationProperty);
        NSLog(@"Company name:%@", tmpCompanyname);
        [tmpCompanyname release];
        
        //获取的联系人单一属性:Job Title
        NSString* tmpJobTitle= (NSString*)ABRecordCopyValue(tmpPerson, kABPersonJobTitleProperty);
        NSLog(@"Job Title:%@", tmpJobTitle);
        [tmpJobTitle release];
        
        //获取的联系人单一属性:Department name
        NSString* tmpDepartmentName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonDepartmentProperty);
        NSLog(@"Department name:%@", tmpDepartmentName);
        [tmpDepartmentName release];
        
        //获取的联系人单一属性:Email(s)
        ABMultiValueRef tmpEmails = ABRecordCopyValue(tmpPerson, kABPersonEmailProperty);
        for(NSInteger j = 0; ABMultiValueGetCount(tmpEmails); j++)
        {
            NSString* tmpEmailIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);
            NSLog(@"Emails%d:%@", j, tmpEmailIndex);
            [tmpEmailIndex release];
        }
        CFRelease(tmpEmails);
        
        //获取的联系人单一属性:Birthday
        NSDate* tmpBirthday = (NSDate*)ABRecordCopyValue(tmpPerson, kABPersonBirthdayProperty);
        NSLog(@"Birthday:%@", tmpBirthday);
        [tmpBirthday release];
        
        //获取的联系人单一属性:Note
        NSString* tmpNote = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNoteProperty);
        NSLog(@"Note:%@", tmpNote);
        [tmpNote release];
        
        //获取的联系人单一属性:Generic phone number
        ABMultiValueRef tmpPhones = ABRecordCopyValue(tmpPerson, kABPersonPhoneProperty);
        for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
        {
            NSString* tmpPhoneIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
            NSLog(@"tmpPhoneIndex%d:%@", j, tmpPhoneIndex);
            [tmpPhoneIndex release];
        }
        CFRelease(tmpPhones);
    }
    
    //释放内存
    [tmpPeoples release];
    CFRelease(tmpAddressBook);
}

iOS通过app读取通讯录信息(整理),布布扣,bubuko.com

iOS通过app读取通讯录信息(整理)

标签:ios   通讯录   addressbook   addressbookui   abaddressbookref   

原文地址:http://blog.csdn.net/u011439689/article/details/26478435

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!