IOS 使用通讯录
1 添加 AddressBook.framewor
AddressBookUI.framework
2 #import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
3 检查授权
// iOS 6
-(bool)checkAddressBookAuthorizationStatus:(UITableView*)tableView;
{
ABAuthorizationStatus authStatus =
ABAddressBookGetAuthorizationStatus();
if (authStatus != kABAuthorizationStatusAuthorized)
{
ABAddressBookRequestAccessWithCompletion
(addressBook, ^(bool granted, CFErrorRef error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
NSLog(@"Error: %@", (__bridge NSError *)error);
else if (!granted) {
UIAlertView *av = [[UIAlertView alloc]
initWithTitle:@"Authorization Denied"
message:@"Set permissions in Settings>General>Privacy."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[av show];
}
else
{
// TODO
}
});
});
}
return authStatus == kABAuthorizationStatusAuthorized;
}
4 Demo
ABAddressBookRef addressBook = ABAddressBookCreate();//定义通讯录名字为addressbook
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);//将通讯录中的信息用数组方式读出
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);//获取通讯录中联系人count。也可以这样 CFArrayGetCount(contacts)
for (int i = 0; i < nPeople; i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(contacts, i); // 获取一个联系人
//读取firstname
NSString *personName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
//读取lastname
NSString *lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
//读取middlename
NSString *middlename = (__bridge NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
//读取prefix前缀
NSString *prefix = (__bridge NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);
//读取suffix后缀
NSString *suffix = (__bridge NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);
//读取nickname呢称
NSString *nickname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);
//读取firstname拼音音标
NSString *firstnamePhonetic = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);
//读取lastname拼音音标
NSString *lastnamePhonetic = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);
//读取middlename拼音音标
NSString *middlenamePhonetic = (__bridge NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);
//读取organization公司
NSString *organization = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
//读取jobtitle工作
NSString *jobtitle = (__bridge NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);
//读取department部门
NSString *department = (__bridge NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);
//读取birthday生日
NSDate *birthday = (__bridge NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
//读取note备忘录
NSString *note = (__bridge NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);
//第一次添加该条记录的时间
NSString *firstknow = (__bridge NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
//最后一次修改該条记录的时间
NSString *lastknow = (__bridge NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
//获取email多值
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int emailcount = ABMultiValueGetCount(email);
for (int x = 0; x < emailcount; x++)
{
//获取email Label
NSString* emailLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));
//获取email值
NSString* emailContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(email, x);
}
//读取地址多值
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
int count = ABMultiValueGetCount(address);
for(int j = 0; j < count; j++)
{
//获取地址Label
NSString* addressLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(address, j);
//获取該label下的地址6属性
NSDictionary* personaddress =(__bridge NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);
NSString* country = [personaddress valueForKey:(__bridge NSString *)kABPersonAddressCountryKey];
NSString* city = [personaddress valueForKey:(__bridge NSString *)kABPersonAddressCityKey];
NSString* state = [personaddress valueForKey:(__bridge NSString *)kABPersonAddressStateKey];
NSString* street = [personaddress valueForKey:(__bridge NSString *)kABPersonAddressStreetKey];
NSString* zip = [personaddress valueForKey:(__bridge NSString *)kABPersonAddressZIPKey];
NSString* coutntrycode = [personaddress valueForKey:(__bridge NSString *)kABPersonAddressCountryCodeKey];
}
//获取dates多值
ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
int datescount = ABMultiValueGetCount(dates);
for (int y = 0; y < datescount; y++)
{
//获取dates Label
NSString* datesLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));
//获取dates值
NSString* datesContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(dates, y);
}
//获取kind值
CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);
if (recordType == kABPersonKindOrganization) {
// it's a company
NSLog(@"it's a company\n");
} else {
// it's a person, resource, or room
NSLog(@"it's a person, resource, or room\n");
}
//获取IM多值
ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);
for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)
{
//获取IM Label
NSString* instantMessageLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);
//获取該label下的2属性
NSDictionary* instantMessageContent =(__bridge NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);
NSString* username = [instantMessageContent valueForKey:(__bridge NSString *)kABPersonInstantMessageUsernameKey];
NSString* service = [instantMessageContent valueForKey:(__bridge NSString *)kABPersonInstantMessageServiceKey];
}
//读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phone); k++)
{
//获取电话Label
NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
}
//获取URL多值
ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
for (int m = 0; m < ABMultiValueGetCount(url); m++)
{
//获取电话Label
NSString * urlLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));
//获取該Label下的电话值
NSString * urlContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(url,m);
}
//读取照片
NSData *image = (__bridge NSData*)ABPersonCopyImageData(person);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)];
[myImage setImage:[UIImage imageWithData:image]];
myImage.opaque = YES;
}
CFRelease(results);
CFRelease(addressBook);
posted on 2014-01-23 16:21 MoreNotepad++ 阅读(156) 评论(0) 收藏 举报
浙公网安备 33010602011771号