你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開辟中使app獲得本機通信錄的完成代碼實例

iOS開辟中使app獲得本機通信錄的完成代碼實例

編輯:IOS開發綜合

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

2、獲得通信錄

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

3、顯示在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;
}

列表後果

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615465850.png (314×132)

PS:通信錄中德律風號碼中的"-"可以在存入數組之進步行處置,屬於NSString處置的領域,處理方法有許多種,本文不多加解釋

4、刪除通信錄數據

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

【iOS開辟中使app獲得本機通信錄的完成代碼實例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved