gpt4 book ai didi

iOS通讯录列表,只返回一组,不是全部

转载 作者:行者123 更新时间:2023-11-29 03:00:12 25 4
gpt4 key购买 nike

我正在编写代码,试图解析 IOS 地址簿联系人列表中的所有联系人。这看起来很简单,我已经将联系人和各自的电子邮件作为列表返回。

我注意到的一件事是我的 Iphone 联系人列表中有多个群组(我在浏览手机的 native 联系人列表时看到了这个):

  • 所有 Facebook
  • 所有 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.
}

最佳答案

从我的工作应用程序中的代码中,要获取地址簿中的所有联系人,请使用以下代码:

ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, nil);
NSMutableArray *contactRefs = [NSMutableArray array];
NSArray *sources = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllSources(book));
for (id source in sources){
NSArray *refs = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(book, (__bridge ABRecordRef)(source), ABPersonGetSortOrdering()));
[contactRefs addObjectsFromArray:refs];
}

基本上,这样做是遍历所有组,并将所有组中的联系人添加到 contactRefs

关于iOS通讯录列表,只返回一组,不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23401977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com