【发布时间】:2014-05-01 04:25:43
【问题描述】:
我正在编写尝试解析 IOS 通讯录联系人列表中的所有联系人的代码。这看起来很简单,我已经将联系人和相应的电子邮件作为列表返回。
我注意到的一件事是我的 Iphone 联系人列表中有多个组(我看到这个浏览手机的本地联系人列表):
- 所有脸书
- 所有 Gmail
- 全部在我的 iPhone 上
我的手机当前忽略 Facebook,并在我的列表中显示另外两个。
当我查看代码联系人的输出时,对于我的应用,我意识到默认情况下它只显示“所有 Gmail”的联系人。
我还需要做些什么来显示“我的 iPhone 上的所有内容”联系人吗?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
[self addContactToAddressBook:addressBookRef];
} else {
// User denied access
// Display an alert telling user the contact could not be added
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Contact list could not be loaded, since you denied access" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self addContactToAddressBook:addressBookRef];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please grant access to your contact list in Iphone Settings" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
-(void)addContactToAddressBook:(ABAddressBookRef)addressBook {
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
//CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
CFIndex nPeople = CFArrayGetCount(allPeople);
self.allContacts = [[NSMutableArray alloc] init];
// int contactIndex = 0;
for (int i = 0; i < nPeople; i++) {
// Get the next address book record.
ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName, lastName, companyName;
firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(record, kABPersonLastNameProperty);
companyName = ABRecordCopyValue(record, kABPersonOrganizationProperty);
NSString *fullName = nil;
if(firstName == nil && lastName == nil){
fullName = [NSString stringWithFormat:@"%@", companyName];
} else {
if(firstName == nil){
fullName = [NSString stringWithFormat:@"%@", lastName];
} if(lastName == nil){
fullName = [NSString stringWithFormat:@"%@", firstName];
} else {
fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
}
// Get array of email addresses from address book record.
ABMultiValueRef emailMultiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
NSArray *emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
dict[@"fullname"] = fullName;
dict[@"emailArr"] = (emailArray != nil ? emailArray : @[]);
// if(emailArray != nil){
[self.allContacts addObject:dict];
//}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
【问题讨论】:
标签: ios addressbook abaddressbook