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

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读取通讯录信息(整理),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。