如何知道此块何时完成执行

时间:2013-04-09 01:42:32

标签: iphone ios ios6 abaddressbook

我正在尝试基于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。找不到任何有用的东西

1 个答案:

答案 0 :(得分:0)

我认为您错误地创建了地址簿。尝试使用以下方法创建地址簿:

 ABAddressBookRef addressBook = ABAddressBookCreate();

您不需要在第一行声明为NULL,因此请将其删除。然后尝试获取person对象,看看它是否返回有效的引用。

另外,我相信ABAddressBookCreateWithOptions的参数如下:

ABAddressBookRef ABAddressBookCreateWithOptions (
   CFDictionaryRef options,
   CFErrorRef* error
);

但不是您在传递地址簿时传递的选项。我ABAddressBookCreateWithOptions运气不好,所以我只使用ABAddressBookCreate。它在iOS 6中被弃用,但对我来说这不是一个交易破坏者。

相关问题