码迷,mamicode.com
首页 > 移动开发 > 详细

iOS电话簿导入代码

时间:2014-05-09 12:27:46      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

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

iOS电话簿导入代码

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/xlhu/p/3708872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!