2013-04-09 25 views
0

我想基於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只在塊的執行後加載/更新。找不到有用的東西

+0

'person'對象是他第一個有效的對象嗎? – 2013-04-09 02:44:29

+0

否。人員對象無效。但是我做錯了什麼? – 2013-04-09 03:26:14

回答

0

我認爲你是在創建你的地址簿錯誤。嘗試用這種創造你的通訊錄

ABAddressBookRef addressBook = ABAddressBookCreate(); 

你並不需要聲明的第一行空,因此刪除。然後嘗試獲取person對象並查看它是否返回有效的引用。

而且,我相信ABAddressBookCreateWithOptions具有參數如下:

ABAddressBookRef ABAddressBookCreateWithOptions (
    CFDictionaryRef options, 
    CFErrorRef* error 
); 

而是選擇你傳遞到地址簿中的參考。我沒有太多運氣,所以我只用ABAddressBookCreate。它在iOS 6中已被棄用,但對我來說這不是一個破壞行爲。

+0

xcode表示ABAddressBookCreate()在IOS6中被棄用 – 2013-04-09 03:59:08

+1

嘗試'ABAddressBookCreateWithOptions(NULL,NULL)' – 2013-04-09 04:03:19

+0

是的。明白了吧。但問題更多的是將我的數據庫中的recordID轉換爲ABRecordID。 – 2013-04-09 04:29:51

相關問題