iOS.주소록 액세스 1.01연락처 정보 읽기

21681 단어 ios
1. 관련 함수 소개
1、 



ABAddressBookRef ABAddressBookCreateWithOptions(



  CFDictionaryRef options,



  CFErrorRef *error



);



 :



CFErrorRef error = NULL;



ABAdressBookRef addressBook = ABAdressBookCreateWithOptions(NULL,&error);



ABAddressBookRequestAccessWithCompletion(addressBook,^(bool granted,CFErrorRef error){



  if(granted){



    //   ......



  }



});



CFRelease(addressBook);



2、 



CFArrayRef ABAddressBookCopyArrayOfAllPeople(



  ABAddressBookRef addressBook



);



CFArrayRef ABAddressBookCopyPeopleWithName(



  ABAddressBookRef addressBook,



  CFStringRef name



);



 :



CFErrorRef error = NULL;



ABAdressBookRef addressBook = ABAdressBookCreateWithOptions(NULL,&error);



NSArray *listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));



3、 



CFTypeRef ABRecordCopyValue(



  ABRecordRef record,



  ABPropertyID property



);



 :



ABRecordRef thisPerson = CFBridgingRetain(listContacts objectAtIndex:0);



NSString *firstName  = CFBridgingRelease(ABRecordCopyValue(thisPerson,kABPersonFirstNameProperty));



NSString *lastName = CFBridgingRelease(ABRecordCopyValue(thisPerson,kABPersonLastNameProperty));



CFRelease(thisPerson);



4、 



 ABRecordCopyValue, ABMultiValueRef:



ABMultiValueRef ABRecordCopyValue(



  ABRecordRef record,



  ABPropertyID property



);



 :



ABRecordRef thisPerson = ABAddressBookGetPersonWithRecordID(addressBook,personID);



ABMultiValueRef emailsProperty = ABRecordCopyValue(thisPerson,kABPersonEmailProperty));



5、 ABMultiValueRef CFArrayRef 



CFArrayRef ABMultiValueCopyArrayOfAllValues(



  ABMultiValueRef multiValue



);



 :



NSArray *emailsArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emailsProperty));



6、 ABMultiValueRef CFStringRef 



CFStringRef ABMultiValueCopyLabelAtIndex(



  ABMultiValueRef multiValue,



  CFIndex index



);



 :



NSString *emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailsProperty,index));



7、 



CFDataRef ABPersonCopyImageData(



  ABRecordRef person



); 



 :



if(ABPersonHasImageData(person)){



  NSData *photoData = CFBridgingRelease(ABPersonCopyImageData(person));



  if(photoData){



    ......

  }



}

2. 사례 코드
#import <UIKit/UIKit.h>

#import <AddressBook/AddressBook.h>



#import "T20140622175801DetailViewController.h"



@interface T20140622175801ViewController : UITableViewController

<UISearchBarDelegate, UISearchDisplayDelegate>



@property (nonatomic, strong) NSArray *listContacts;



- (void)filterContentForSearchText:(NSString*)searchText;



@end
#import "T20140622175801ViewController.h"



@interface T20140622175801ViewController ()



@end



@implementation T20140622175801ViewController



- (void)viewDidLoad

{

    [super viewDidLoad];

    

    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

        

        if (granted) {

            // 

            [self filterContentForSearchText:@""];

        }

        

    });

    

    CFRelease(addressBook);

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



- (void)filterContentForSearchText:(NSString*)searchText

{

    // 

    if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {

        return ;

    }

    

    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    

    

    if([searchText length]==0)

    {

        // 

        self.listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

    } else {

        // 

        CFStringRef cfSearchText = (CFStringRef)CFBridgingRetain(searchText);

        self.listContacts = CFBridgingRelease(ABAddressBookCopyPeopleWithName(addressBook, cfSearchText));

        CFRelease(cfSearchText);

    }

    

    [self.tableView  reloadData];

    

    CFRelease(addressBook);

    

    

}



#pragma mark - Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [self.listContacts count];

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        

    }

    

    ABRecordRef thisPerson = CFBridgingRetain([self.listContacts objectAtIndex:[indexPath row]]);

    

    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty));

    firstName = firstName != nil?firstName:@"";

    NSString *lastName =  CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonLastNameProperty));

    lastName = lastName != nil?lastName:@"";

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",firstName,lastName];

    NSString* name = CFBridgingRelease(ABRecordCopyCompositeName(thisPerson));

    cell.textLabel.text = name;

    

    

    CFRelease(thisPerson);

    

    return cell;

}





- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        

        ABRecordRef thisPerson = CFBridgingRetain([self.listContacts objectAtIndex:[indexPath row]]);

        T20140622175801DetailViewController *detailViewController = [segue destinationViewController];

        

        ABRecordID personID = ABRecordGetRecordID(thisPerson);

        NSNumber *personIDAsNumber = [NSNumber numberWithInt:personID];

        detailViewController.personIDAsNumber = personIDAsNumber;

        

        CFRelease(thisPerson);

    }

}



#pragma mark --UISearchBarDelegate  

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{

    // 

    [self filterContentForSearchText:@""];

}





#pragma mark - UISearchDisplayController Delegate Methods

//

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{

    [self filterContentForSearchText:searchString];

    //YES 

    return YES;

}





@end
#import <UIKit/UIKit.h>

#import <AddressBook/AddressBook.h>



@interface T20140622175801DetailViewController : UITableViewController





@property (weak, nonatomic) IBOutlet UIImageView *imageView;



@property (weak, nonatomic) IBOutlet UILabel *lblName;

@property (weak, nonatomic) IBOutlet UILabel *lblMobile;



@property (weak, nonatomic) IBOutlet UILabel *lblIPhone;

@property (weak, nonatomic) IBOutlet UILabel *lblWorkEmail;

@property (weak, nonatomic) IBOutlet UILabel *lblHomeEmail;



@property (strong, nonatomic) NSNumber* personIDAsNumber;



@end
#import "T20140622175801DetailViewController.h"



@interface T20140622175801DetailViewController ()



@end



@implementation T20140622175801DetailViewController



- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

    ABRecordID personID = [self.personIDAsNumber intValue];

    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    

    

    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID);

    

    // 

    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));

    firstName = firstName != nil?firstName:@"";

    NSString *lastName =  CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));

    lastName = lastName != nil?lastName:@"";

    [self.lblName setText: [NSString stringWithFormat:@"%@ %@",firstName,lastName]];

    NSString* name = CFBridgingRelease(ABRecordCopyCompositeName(person));

    [self.lblName setText: name];

    

    // Email 

    ABMultiValueRef emailsProperty = ABRecordCopyValue(person, kABPersonEmailProperty);

    NSArray* emailsArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emailsProperty));

    for(int index = 0; index< [emailsArray count]; index++){

        NSString *email = [emailsArray objectAtIndex:index];

        NSString *emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailsProperty, index));

        

        if ([emailLabel isEqualToString:(NSString*)kABWorkLabel]) {

            [self.lblWorkEmail setText:email];

        } else if ([emailLabel isEqualToString:(NSString*)kABHomeLabel]) {

            [self.lblHomeEmail setText:email];

        } else {

            NSLog(@"%@: %@", @" Email", email);

        }

    }

    CFRelease(emailsProperty);

    

    // 

    ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);

    NSArray* phoneNumberArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNumberProperty));

    for(int index = 0; index< [phoneNumberArray count]; index++){

        NSString *phoneNumber = [phoneNumberArray objectAtIndex:index];

        NSString *phoneNumberLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phoneNumberProperty, index));

        

        if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneMobileLabel]) {

            [self.lblMobile setText:phoneNumber];

        } else if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {

            [self.lblIPhone setText:phoneNumber];

        } else {

            NSLog(@"%@: %@", @" ", phoneNumber);

        }

    }

    CFRelease(phoneNumberProperty);

    

    // 

    if (ABPersonHasImageData(person)) {

        NSData *photoData = CFBridgingRelease(ABPersonCopyImageData(person));

        if(photoData){

            [self.imageView setImage:[UIImage imageWithData:photoData]];

        }

    }

    

    CFRelease(addressBook);

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}





@end

좋은 웹페이지 즐겨찾기