我正在尝试根据 recordID 访问一个人的 ABAddressBook 属性,但是每次我尝试获取一个属性时,我都会得到一个空值。
这是我尝试过的代码。
ABAddressBookRef addressBook = NULL;
addressBook = ABAddressBookCreateWithOptions(addressBook, nil);
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, (ABRecordID) [[object valueForKey:@"recordId"] description]);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"First Name %@", firstName);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *name = [NSString stringWithFormat:@"%@" "%@", lastName, firstName];
NSLog(@"Full Name %@", name);
我在这里做错了什么?
编辑:我已经想通了上述问题。这是我用过的。现在我遇到的问题是,只有当这段代码完成执行时,我才需要重新加载我的 UITableView。但我无法弄清楚这个块何时完成执行。需要帮助。
CFErrorRef myError = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &myError);
ABAddressBookRequestAccessWithCompletion(addressBook,
^(bool granted, CFErrorRef error) {
if (granted) {
ABRecordID recID = [recordId intValue];
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, recID);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
if(!firstName){
firstName = @"";
}
if(!lastName){
lastName = @"";
}
NSString *name = [NSString stringWithFormat:@"%@ %@", lastName, firstName];
if([firstName length] < 1 && [lastName length] < 1){
name = @"No Name";
}
self.personName = name;
NSData *imageData = (NSData *)CFBridgingRelease(ABPersonCopyImageData(person));
if(imageData){
self.personImage = [UIImage imageWithData:imageData];
}
else{
self.personImage = [UIImage imageNamed:@"default.png"];
}
NSMutableArray *array = [NSMutableArray array];
[array addObject:self.personName];
[array addObject:self.personImage];
[self.personDetailsArray addObject:array];
[self.tableView reloadData];
} else {
// Handle the error
}
});
更新块内的 UIElements 是否正确?如果不是,还有什么选择。
我想要的是仅在执行块后才加载/更新我的 UITableView。还没有找到任何有用的东西