iOS通過app讀取通訊錄資訊(整理)

來源:互聯網
上載者:User

iOS通過app讀取通訊錄資訊,讀取通訊錄資訊時需要載入AddressBookUI 和AddressBook兩個包,並且引入標頭檔
#import
#import
具體實現如下:

-(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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.