iOS電話簿匯入代碼

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

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