2009-12-08 78 views
1

我已經能夠使用SDK訪問人員的地址簿聯繫人,但每當我選擇一個公司的聯繫人時,我的應用程序崩潰。iPhone SDK訪問地址簿公司聯繫人

有沒有人知道公司領域的財產? 有沒有人知道代碼,使其工作?

在此先感謝

+0

這是您的代碼有問題,但您還沒有發佈任何內容。 – wkw 2009-12-08 10:48:38

+0

當你獲取聯繫人的名字時,你不會檢查NULL。 – 2012-05-26 20:18:51

回答

0

你需要使用kABPersonOrganizationProperty屬性得到通訊錄

1

公司名稱看來你的問題是不相關的公司標籤本身。 AB字段需要仔細管理,不用時釋放並且一直檢查爲零,許多字段沒有設置。

原則上,這裏是我如何使用它的例子。這個函數加載聯繫人(對我的應用感興趣的那些字段),並返回數組和聯繫人給調用者。希望這是有幫助的。請注意我如何發佈不需要的參考。

- (NSArray *)loadContacts 
{ 
     ABAddressBookRef addressBook = ABAddressBookCreate(); 
     CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); 
     CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 
     NSMutableArray *contacts = [NSMutableArray array]; 

     for (int i = 0 ; i < nPeople ; i++) { 
       ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
       NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 
       NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); 

       NSString *fullName; 

       if (firstName && lastName) { 
         fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
       } else { 
         if (firstName) { 
           fullName = [NSString stringWithString:firstName]; 
         } else if (lastName) { 
           fullName = [NSString stringWithString:lastName]; 
         } else { 
           continue; 
         } 
       } 

       NSString *email = nil; 
       ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 

       if (emails) { 
         NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emails) autorelease]; 
         if (emailAddresses && [emailAddresses count] > 0) 
           email = [emailAddresses objectAtIndex:0]; 
         CFRelease(emails); 
       } 
       if (email) { 
         NSDictionary *contact = [NSDictionary 
               dictionaryWithObjectsAndKeys:fullName, @"name", 
               email, @"email", nil]; 
         [contacts addObject:contact]; 
       } 
       if (firstName) 
         CFRelease(firstName); 
       if (lastName) 
         CFRelease(lastName); 
     } 

     CFRelease(allPeople); 
     CFRelease(addressBook); 
     return contacts; 
} 
相關問題