iOS開(kāi)發(fā)中使app獲取本機(jī)通訊錄的實(shí)現(xiàn)代碼實(shí)例
一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、獲取通訊錄
1、在infterface中定義數(shù)組并在init方法中初始化
NSMutableArray *addressBookTemp;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
addressBookTemp = [NSMutableArray array];
}
2、定義一個(gè)model,用來(lái)存放通訊錄中的各個(gè)屬性
新建一個(gè)繼承自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文件中進(jìn)行synthesize
@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
@end
3、獲取聯(lián)系人
在iOS6之后,獲取通訊錄需要獲得權(quán)限
//新建一個(gè)通訊錄類
ABAddressBookRef addressBooks = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
{
addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
//獲取通訊錄權(quán)限
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
else
{
addressBooks = ABAddressBookCreate();
}
//獲取通訊錄中的所有人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
//通訊錄中人數(shù)
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
//循環(huán),獲取每個(gè)人的個(gè)人信息
for (NSInteger i = 0; i < nPeople; i++)
{
//新建一個(gè)addressBook model類
TKAddressBook *addressBook = [[TKAddressBook alloc] init];
//獲取個(gè)人
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//獲取個(gè)人名字
CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef abFullName = ABRecordCopyCompositeName(person);
NSString *nameString = (__bridge NSString *)abName;
NSString *lastNameString = (__bridge NSString *)abLastName;
if ((__bridge id)abFullName != nil) {
nameString = (__bridge NSString *)abFullName;
} else {
if ((__bridge id)abLastName != nil)
{
nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
}
}
addressBook.name = nameString;
addressBook.recordID = (int)ABRecordGetRecordID(person);;
ABPropertyID multiProperties[] = {
kABPersonPhoneProperty,
kABPersonEmailProperty
};
NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
ABPropertyID property = multiProperties[j];
ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
NSInteger valuesCount = 0;
if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
if (valuesCount == 0) {
CFRelease(valuesRef);
continue;
}
//獲取電話號(hào)碼和email
for (NSInteger k = 0; k < valuesCount; k++) {
CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
switch (j) {
case 0: {// Phone number
addressBook.tel = (__bridge NSString*)value;
break;
}
case 1: {// Email
addressBook.email = (__bridge NSString*)value;
break;
}
}
CFRelease(value);
}
CFRelease(valuesRef);
}
//將個(gè)人信息添加到數(shù)組中,循環(huán)完成后addressBookTemp中包含所有聯(lián)系人的信息
[addressBookTemp addObject:addressBook];
if (abName) CFRelease(abName);
if (abLastName) CFRelease(abLastName);
if (abFullName) CFRelease(abFullName);
}
三、顯示在table中
//行數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//列數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [addressBookTemp count];
}
//cell內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"ContactCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
cell.textLabel.text = book.name;
cell.detailTextLabel.text = book.tel;
return cell;
}
列表效果
PS:通訊錄中電話號(hào)碼中的"-"可以在存入數(shù)組之前進(jìn)行處理,屬于NSString處理的范疇,解決辦法有很多種,本文不多加說(shuō)明
四、刪除通訊錄數(shù)據(jù)
<span style="white-space:pre"> </span>ABRecordRef person = objc_unretainedPointer([myContacts objectAtIndex:indexPath.row]);
CFErrorRef *error;
ABAddressBookRemoveRecord(addressBook, person, error);
ABAddressBookSave(addressBook, error);
myContacts = nil;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- 總結(jié)iOS App開(kāi)發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
- iOS App初次啟動(dòng)時(shí)的用戶引導(dǎo)頁(yè)制作實(shí)例分享
- iOS保存App中的照片到系統(tǒng)相冊(cè)或自建相冊(cè)的方法
- iOS App中調(diào)用相冊(cè)中圖片及獲取最近的一張圖片的方法
- 詳解在iOS App中自定義和隱藏狀態(tài)欄的方法
- safari調(diào)試iOS app web頁(yè)面的步驟
- iOS App開(kāi)發(fā)中使cell高度自適應(yīng)的黑魔法詳解
- 詳解iOS App開(kāi)發(fā)中Cookie的管理方法
- IOS App圖標(biāo)和啟動(dòng)畫(huà)面尺寸詳細(xì)介紹
- iOS開(kāi)發(fā)教程之APP內(nèi)部切換語(yǔ)言的實(shí)現(xiàn)方法
相關(guān)文章
iOS的CoreAnimation開(kāi)發(fā)框架中的Layer層動(dòng)畫(huà)制作解析
在iOS中UIView層的屬性會(huì)映射到CoreAnimation框架的CALayer,這里我們來(lái)看一下iOS的CoreAnimation開(kāi)發(fā)框架中的Layer層動(dòng)畫(huà)制作解析,需要的朋友可以參考下2016-07-07iOS安全防護(hù)系列之重簽名防護(hù)與sysctl反調(diào)試詳解
這篇文章主要給大家介紹了關(guān)于iOS安全防護(hù)系列之重簽名防護(hù)與sysctl反調(diào)試的相關(guān)資料,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07iOS的UI開(kāi)發(fā)中UITabBarControlle的基本使用教程
這篇文章主要介紹了iOS的UI開(kāi)發(fā)中UITabBarControlle的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS 斷點(diǎn)上傳文件的實(shí)現(xiàn)方法
這項(xiàng)目開(kāi)發(fā)中,有時(shí)候我們需要將本地的文件上傳到服務(wù)器,簡(jiǎn)單的幾張圖片還好,但是針對(duì)iPhone里面的視頻文件進(jìn)行上傳,為了用戶體驗(yàn),我們有必要實(shí)現(xiàn)斷點(diǎn)上傳。這篇文章主要介紹了iOS 斷點(diǎn)上傳文件的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-12-12iOS+PHP注冊(cè)登錄系統(tǒng) iOS部分(下)
這篇文章主要介紹了iOS+PHP注冊(cè)登錄系統(tǒng)的iOS部分,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12