您好,登錄后才能下訂單哦!
IOS 通訊錄的訪問和修改的實現(xiàn)
在iOS端可以通過AddressBook或者AddressBookUI兩個框架實現(xiàn),區(qū)別是第二個框架帶視圖,一般使用第一個框架就夠了。
下面介紹AddressBook框架的使用,注意這個是C語言框架,使用起來比較麻煩。
①請求授權(quán)
先判斷授權(quán)狀態(tài),如果未授權(quán)則進(jìn)行授權(quán)。
// 1.主動請求授權(quán),先判斷授權(quán)狀態(tài) NSInteger state = ABAddressBookGetAuthorizationStatus(); if (state == kABAuthorizationStatusAuthorized) { NSLog(@"已經(jīng)授權(quán)"); }else{ ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) { if (granted) { NSLog(@"授權(quán)成功"); }else{ NSLog(@"授權(quán)失敗"); } }); }
②獲取通訊錄的聯(lián)系人
c語言沒有對象,通過一些以Ref結(jié)尾的指針來獲取數(shù)據(jù)。
注意C語言數(shù)組的規(guī)模通過函數(shù)獲取,拿到元素也要通過函數(shù)。
注意一些枚舉值以k開頭。
注意電話是一個數(shù)組。
// 先判斷授權(quán)狀態(tài) if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) { NSLog(@"未授權(quán)"); return; } ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); CFIndex count = CFArrayGetCount(persons); for (NSInteger i = 0; i < count; i++) { ABRecordRef person = CFArrayGetValueAtIndex(persons, i); CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); NSLog(@"聯(lián)系人:%@ %@",lastName,firstName); NSInteger cnt = ABMultiValueGetCount(phones); for (int j = 0; j < cnt; j++) { CFStringRef phone = ABMultiValueCopyValueAtIndex(phones, j); NSLog(@"電話:%@ ",phone); } }
③修改通訊錄聯(lián)系人
首先拿到通訊錄中的人,然后調(diào)用SetValue方法來修改值。
最后不要忘記保存。
// 先判斷授權(quán)狀態(tài) if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) { NSLog(@"未授權(quán)"); return; } ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); ABRecordRef person = CFArrayGetValueAtIndex(persons, 0); ABRecordSetValue(person, kABPersonLastNameProperty, @"changed", NULL); ABAddressBookSave(book, NULL);
④添加一個聯(lián)系人
注意添加電話時應(yīng)創(chuàng)建MultiValue。
// 先判斷授權(quán)狀態(tài) if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) { NSLog(@"未授權(quán)"); return; } ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); CFIndex count = CFArrayGetCount(persons); for (NSInteger i = 0; i < count; i++) { ABRecordRef person = CFArrayGetValueAtIndex(persons, i); CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); NSLog(@"聯(lián)系人:%@ %@",lastName,firstName); NSInteger cnt = ABMultiValueGetCount(phones); for (int j = 0; j < cnt; j++) { CFStringRef phone = ABMultiValueCopyValueAtIndex(phones, j); NSLog(@"電話:%@ ",phone); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。