標籤:style blog class code java color
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