gpt4 book ai didi

ios - 我想在 ios 的 tableview 上从电话中获取联系人列表?

转载 作者:行者123 更新时间:2023-11-29 01:14:04 25 4
gpt4 key购买 nike

这段代码是我写的

联系人列表vc.h

#import <UIKit/UIKit.h>

@interface ContactListVc : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *contactTable;
NSMutableArray *tableData;

}
@property (strong, nonatomic) IBOutlet UITableView *contactTable;
@property (nonatomic, strong) NSMutableArray *tableData;

联系人列表vc.m

#import "ContactListVc.h"
#import <AddressBook/AddressBook.h>
#import "Person.h"

@interface ContactListVc ()

@end

@implementation ContactListVc
@synthesize tableData,contactTable;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
tableData = [[NSMutableArray alloc]init];
contactTable = [[UITableView alloc]init];
contactTable.dataSource = self;
contactTable.delegate = self;
[self getPersonOutOfAddressBook];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Person *person = [self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;


return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (addressBook != nil) {
NSLog(@"Succesful.");
NSLog(@"tabledata %@",tableData);

//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

//4
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,
kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

person.firstName = firstName; person.lastName = lastName;
person.fullName = fullName;

//email
//5
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);

//6
NSUInteger j = 0;
for (j = 0; j < ABMultiValueGetCount(emails); j++) {
NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
if (j == 0) {
person.homeEmail = email;
NSLog(@"person.homeEmail = %@ ", person.homeEmail);
}
else if (j==1) person.workEmail = email;
}

//7
[self.tableData addObject:person];
}

//8
CFRelease(addressBook);
} else {
//9
NSLog(@"Error reading Address Book");
}
}

但是我没有得到联系人列表。只有成功才会出现在控制台窗口中。我在一个联系人选项卡上有 3 个选项卡栏,我想在 tableview 中显示电话的联系人列表。请帮助我,谢谢。

最佳答案

您需要先获得访问 native 数据库的权限...

- (void)requestPermissionForContactsAccessAndFetch
{

ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status != kABAuthorizationStatusAuthorized && status != kABAuthorizationStatusNotDetermined) {
// tell user to enable contacts in privacy settings
NSLog(@"You previously denied access: You must enable access to contacts in settings");

return;
}

CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
return;
}

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
}

if (granted) {
[self getContactsFromAddressBook:addressBook];
} else {
// tell user to enable contacts in privacy settings
NSLog(@"You just denied access: You must enable access to contacts in settings");
}

CFRelease(addressBook);
});
}

然后就可以得到一个数组中的所有联系人...

- (NSMutableArray*)getContactsFromAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allData = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger contactCount = [allData count];

for (int i = 0; i < contactCount; i++) {
ABRecordRef person = CFArrayGetValueAtIndex((__bridge CFArrayRef)allData, i);

NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
if (firstName) {
dictionary[@"firstName"] = firstName;
}
if (lastName) {
dictionary[@"lastName"] = lastName;
}

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phones);

if (phoneNumberCount > 0) {
NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, 0));
dictionary[@"phone"] = phone;
}

// or if you wanted to iterate through all of them, you could do something like this...
// for (int j = 0; j < phoneNumberCount; j++) {
// NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, j));
// }

if (phones) {
CFRelease(phones);
}

[arrOfContacts addObject:dictionary];
}
}

关于ios - 我想在 ios 的 tableview 上从电话中获取联系人列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35425114/

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