ios读取通讯录信息
来源:互联网 发布:手机淘宝怎样查看物流 编辑:程序博客网 时间:2024/05/21 22:55
iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和AddressBook两个包,具体实现方法如下
ABAddressBookRef addressBook = ABAddressBookCreate();//定义通讯录名字为addressbook
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);//将通讯录中的信息用数组方式读出
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);//获取通讯录中联系人
iphoneContactList = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < nPeople; i++)
{
IphoneContact * iphoneContact = [[IphoneContact alloc] init];
NSData *imageData = [[[NSData alloc] init]autorelease];
NSString *address = [[[NSString alloc] init]autorelease];
ABRecordRef person = CFArrayGetValueAtIndex(contacts, i);//取出某一个人的信息
NSInteger lookupforkey =(NSInteger)ABRecordGetRecordID(person);//读取通讯录中联系人的唯一标识
NSDate * createDate = (NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty);// 读取通讯录中联系人的创建日期
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString* birthDay = [formatter stringFromDate:createDate];
[formatter release];
NSString *createDate1 = [birthDay stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *createDate2 = [createDate1 stringByReplacingOccurrencesOfString:@":" withString:@""];
NSString *createDate3 = [createDate2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"1111========%@",createDate3);
NSLog(@"222222========%@",birthDay);
NSString *indexInIphone = [NSString stringWithFormat:@"%i",lookupforkey];
iphoneContact.lookUpKey = [createDate3 stringByAppendingString:indexInIphone];
//上诉操作是将某个联系人的标识号与创建日期进行组合,得到唯一的标识,是为了当时特殊的需要,一般不会有这种变态应用,这是因为ABRecordGetRecordID在一个手机中是唯一的,即使删掉某一个联系人,这个号也不会在被占用。
//读取联系人姓名属性
if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))== nil) {
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
ABAddressBookRef addressBook = ABAddressBookCreate();//定义通讯录名字为addressbook