iOS电话簿导入代码

时间:2014-05-09 12:27:46   收藏:0   阅读:335

iOS电话簿导入代码,当前仅仅实现主体框框程序,细节续订;Analysis不会导致内存泄漏

引用

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

代码调试:XCode4.5,iOS6.0

主体代码

 

bubuko.com,布布扣
- (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;
}
bubuko.com,布布扣

 

转至:http://www.cnblogs.com/GoGoagg/archive/2012/12/20/2826804.html

iOS电话簿导入代码,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!