国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發中使app獲取本機通訊錄的實現代碼實例

2020-07-26 03:29:26
字體:
來源:轉載
供稿:網友

一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、獲取通訊錄

1、在infterface中定義數組并在init方法中初始化

復制代碼 代碼如下:

NSMutableArray *addressBookTemp;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    addressBookTemp = [NSMutableArray array];
}

2、定義一個model,用來存放通訊錄中的各個屬性
新建一個繼承自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文件中進行synthesize
復制代碼 代碼如下:

@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
 
@end

3、獲取聯系人

在iOS6之后,獲取通訊錄需要獲得權限

復制代碼 代碼如下:

    //新建一個通訊錄類
    ABAddressBookRef addressBooks = nil;
 
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
 
    {
        addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
 
        //獲取通訊錄權限
 
        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);

//通訊錄中人數
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
 
//循環,獲取每個人的個人信息
for (NSInteger i = 0; i < nPeople; i++)
    {
        //新建一個addressBook model類
        TKAddressBook *addressBook = [[TKAddressBook alloc] init];
        //獲取個人
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        //獲取個人名字
        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;
            }
            //獲取電話號碼和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);
        }
        //將個人信息添加到數組中,循環完成后addressBookTemp中包含所有聯系人的信息
        [addressBookTemp addObject:addressBook];
        
        if (abName) CFRelease(abName);
        if (abLastName) CFRelease(abLastName);
        if (abFullName) CFRelease(abFullName);
    }


三、顯示在table中
復制代碼 代碼如下:

//行數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
//列數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [addressBookTemp count];
}

//cell內容
- (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;
}


列表效果

201611991316031.png (314×132)

PS:通訊錄中電話號碼中的"-"可以在存入數組之前進行處理,屬于NSString處理的范疇,解決辦法有很多種,本文不多加說明

四、刪除通訊錄數據

復制代碼 代碼如下:

<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]; 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇文区| 怀安县| 眉山市| 宝应县| 凌源市| 太保市| 琼海市| 买车| 姜堰市| 紫金县| 德格县| 江孜县| 开鲁县| 隆化县| 鹤庆县| 平阴县| 邓州市| 娱乐| 广东省| 二连浩特市| 山阳县| 贞丰县| 静乐县| 富裕县| 康保县| 樟树市| 青川县| 错那县| 高雄市| 本溪| 岱山县| 富锦市| 将乐县| 栾城县| 丹凤县| 洞头县| 曲阜市| 盐池县| 衡水市| 柳江县| 客服|