我已经能够使用SDK访问人们的地址簿联系人,但每当我选择一个公司的联系人时,我的应用程序崩溃。
有谁知道公司领域的财产? 有没有人知道使其有效的代码?
提前致谢
答案 0 :(得分:1)
您的问题似乎与公司代码本身无关。需要仔细管理AB字段,不使用时释放,并且一直检查nil,不设置多个字段。
原则上,这是我如何使用它的例子。此函数加载联系人(对我的应用程序感兴趣的那些字段)并将带有联系人的数组返回给调用者。希望这是有帮助的。请注意我如何释放不需要的参考文献。
- (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;
}
答案 1 :(得分:0)
您需要使用kABPersonOrganizationProperty
属性从地址簿