首页 > 代码库 > iOS电话簿导入代码
iOS电话簿导入代码
iOS电话簿导入代码,当前仅仅实现主体框框程序,细节续订;Analysis不会导致内存泄漏
引用
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
代码调试:XCode4.5,iOS6.0
主体代码
- (void)testAddress { ABAddressBookRef addressBook = nil; if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) { addressBook = ABAddressBookCreateWithOptions(NULL, NULL); //等待同意后向下执行 dispatch_semaphore_t sema = dispatch_semaphore_create(0); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { dispatch_semaphore_signal(sema); }); dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch_release(sema); } // else // { // addressBook = ABAddressBookCreate(); // } CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); NSLog(@"%@" ,results); int peopleCount = CFArrayGetCount(results); for (int i=0; i<peopleCount; i++) { ABRecordRef record = CFArrayGetValueAtIndex(results, i); NSLog(@"%@" ,record); NSString *fn,*ln,*fullname; fn = ln = fullname = nil; CFTypeRef vtmp = NULL; vtmp = ABRecordCopyValue(record, kABPersonFirstNameProperty); if (vtmp) { fn = [NSString stringWithString:vtmp]; CFRelease(vtmp); vtmp = NULL; } vtmp = ABRecordCopyValue(record, kABPersonLastNameProperty); if (vtmp) { ln = [NSString stringWithString:vtmp]; CFRelease(vtmp); vtmp = NULL; } NSLog(@"%@ ,%@" ,fn ,ln); // 读取电话 ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty); int phoneCount = ABMultiValueGetCount(phones); for (int j=0; j<phoneCount; j++) { // label CFStringRef lable = ABMultiValueCopyLabelAtIndex(phones, j); // phone number CFStringRef phonenumber = ABMultiValueCopyValueAtIndex(phones, j); // localize label CFStringRef ll = ABAddressBookCopyLocalizedLabel(lable); NSLog(@"\t%@ ,%@,%@" ,(NSString *)lable ,(NSString *)ll,(NSString *)phonenumber); if (ll) CFRelease(ll); if (lable) CFRelease(lable); if (phonenumber) CFRelease(phonenumber); } if (phones) CFRelease(phones); record = NULL; } if (results) CFRelease(results); results = nil; if (addressBook) CFRelease(addressBook); addressBook = NULL; }
转至:http://www.cnblogs.com/GoGoagg/archive/2012/12/20/2826804.html
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。