iOS.주소록 액세스 1.01연락처 정보 읽기
21681 단어 ios
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.