我是iphone的新手。在我的项目中,我主要是将所有地址簿联系人(姓名和电子邮件)放在我的项目中创建的表视图中,这些联系人放在iphone设备中。我该怎么办?
答案 0 :(得分:3)
NSMutableArray* contactsArray = [NSMutableArray new];
// open the default address book.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook)
{
NSLog(@"opening address book");
}
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++)
{
NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[tempContactDic setValue:name forKey:@"name"];
//fetch email id
NSString *strEmail;
ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
strEmail = (__bridge NSString *)tempEmailref;
[tempContactDic setValue:strEmail forKey:@"email"];
[contactsArray addObject:tempContactDic];
}
所有联系人数据保存在Contacts Array中。然后你可以根据需要使用这个数组。
答案 1 :(得分:0)
使用addressbook api执行此操作。看到我的回答
Contact list on iPhone without using contact picker
有关详情,请参阅Apple文档here。
答案 2 :(得分:0)
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
-(void)viewWillAppear:(BOOL)animated{
NSMutableArray* contactsArray = [NSMutableArray new];
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook)
{
NSLog(@"opening address book");
}
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++)
{
NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
NSLog(@"tempContactDic ios a ==%@",tempContactDic);
CFStringRef firstName, lastName;
firstName = (__bridge CFStringRef)((__bridge UILabel *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty)));
lastName = (__bridge CFStringRef)((__bridge UILabel *)(ABRecordCopyValue(ref, kABPersonLastNameProperty)));
// [tempContactDic setValue:name forKey:@"name"];
//fetch email id
NSString *strEmail;
ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
strEmail = (__bridge NSString *)tempEmailref;
[tempContactDic setValue:strEmail forKey:@"email"];
[contactsArray addObject:tempContactDic];
}
}
-(IBAction)getcontactlist:(id)sender{
[self getContact];
}
-(IBAction)getContact {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
firstName.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
number.text = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
这段代码对我来说很好......