iOS电话簿导入代码

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

引用

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

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

主体代码:

 1 - (void)testAddress
 2 {
 3     ABAddressBookRef addressBook = nil;
 4 
 5     if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
 6     {
 7         addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
 8         //等待同意后向下执行
 9         dispatch_semaphore_t sema = dispatch_semaphore_create(0);
10         ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
11                                                  {
12                                                      dispatch_semaphore_signal(sema);
13                                                  });
14 
15         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
16         dispatch_release(sema);
17     }
18 //    else
19 //    {
20 //        addressBook = ABAddressBookCreate();
21 //    }
22     
23     CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);
24     
25     NSLog(@"%@" ,results);
26     
27     int peopleCount = CFArrayGetCount(results);
28     
29     for (int i=0; i<peopleCount; i++)
30     {
31         ABRecordRef record = CFArrayGetValueAtIndex(results, i);
32         
33         NSLog(@"%@" ,record);
34         
35         NSString *fn,*ln,*fullname;
36         fn = ln = fullname = nil;
37         
38         CFTypeRef vtmp = NULL;
39         
40         vtmp = ABRecordCopyValue(record, kABPersonFirstNameProperty);
41         if (vtmp)
42         {
43             fn = [NSString stringWithString:vtmp];
44             
45             CFRelease(vtmp);
46             vtmp = NULL;
47         }
48         vtmp = ABRecordCopyValue(record, kABPersonLastNameProperty);
49         if (vtmp)
50         {
51             ln = [NSString stringWithString:vtmp];
52             
53             CFRelease(vtmp);
54             vtmp = NULL;
55         }
56         
57         NSLog(@"%@ ,%@" ,fn ,ln);
58         
59         // 读取电话
60         ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
61         int phoneCount = ABMultiValueGetCount(phones);
62         
63         for (int j=0; j<phoneCount; j++)
64         {
65             // label
66             CFStringRef lable = ABMultiValueCopyLabelAtIndex(phones, j);
67             // phone number
68             CFStringRef phonenumber = ABMultiValueCopyValueAtIndex(phones, j);
69             
70             // localize label
71             CFStringRef ll = ABAddressBookCopyLocalizedLabel(lable);
72             
73             NSLog(@"\t%@ ,%@,%@" ,(NSString *)lable ,(NSString *)ll,(NSString *)phonenumber);
74             
75             if (ll)
76                 CFRelease(ll);
77             if (lable)
78                 CFRelease(lable);
79             if (phonenumber)
80                 CFRelease(phonenumber);
81         }
82         
83         if (phones)
84             CFRelease(phones);
85         
86         record = NULL;
87     }
88     
89     if (results)
90         CFRelease(results);
91     results = nil;
92 
93     if (addressBook)
94         CFRelease(addressBook);
95     addressBook = NULL;
96 }
View Code
- (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;
}

 



另类问题:由于电话号码是从其余地方同步到电话薄,存在手机没有的标签,从而导致内存泄漏

 

 

posted @ 2012-12-20 17:52  西就东城  阅读(3671)  评论(0编辑  收藏  举报