我想基於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只在塊的執行後加載/更新。找不到有用的東西
'person'對象是他第一個有效的對象嗎? – 2013-04-09 02:44:29
否。人員對象無效。但是我做錯了什麼? – 2013-04-09 03:26:14