[轉]iOS技巧之擷取本機通訊錄中的內容,解析通訊錄原始碼

來源:互聯網
上載者:User

標籤:

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

二、擷取通訊錄

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

?
123456 NSMutableArray *addressBookTemp; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    addressBookTemp = [NSMutableArray array];}
2、定義一個model,用來存放通訊錄中的各個屬性

建立一個繼承自NSObject的類,在.h中

?
1234567891011121314 @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?
1234 @implementation TKAddressBook@synthesize name, email, tel, recordID, sectionNumber; @end

3、擷取連絡人

在iOS6之後,擷取通訊錄需要獲得許可權

?
1234567891011121314151617181920212223242526272829     //建立一個通訊錄類    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);
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 //通訊錄中人數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中?
123456789 //行數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;} //列數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [addressBookTemp count];}
?
12345678910111213141516171819 //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;}
列表效果

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

  

[轉]iOS技巧之擷取本機通訊錄中的內容,解析通訊錄原始碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.