我正在获取地址簿中所有联系人的数组:
NSMutableArray *records = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople( addressBook );
谓词用什么格式表示联系人的名字?我按照此问题的建议尝试了记录。:Search ABAddressbook iOS SDK但我得到了一个未知的密钥异常。数组中的项似乎是 __ NSCFType 类型。任何帮助将不胜感激。
答案 0 :(得分:1)
嘿,为了从设备获取联系信息,您可以使用我的波纹管代码以及我创建的Contact bean类,只看到这个..
您还需要包含AddressBook.framework
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABAddressBook.h>
#import <AddressBook/ABPerson.h>
[contactList removeAllObjects];
// open the default address book.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
NSLog(@"opening address book");
}
// can be cast to NSArray, toll-free
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
// CFStrings can be cast to NSString!
for (int i=0;i < nPeople;i++) {
MContact *contact = [[MContact alloc] init];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
contact.name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0) {
contact.email = (NSString *)ABMultiValueCopyValueAtIndex(eMail, 0);
[contactList addObject:contact];
}
CFRelease(ref);
CFRelease(firstName);
CFRelease(lastName);
}
这里的MContact是NObject(Bean)文件,如下所示
@interface MContact : NSObject {
NSString *email;
NSString *name;
NSString *lastName;
NSString *phone;
BOOL isSelected;
}
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *lastName;
@property (nonatomic, retain) NSString *phone;
@property (nonatomic) BOOL isSelected;
@property (nonatomic, readonly) NSString *displayName;
@end
我希望这可以帮助你...
答案 1 :(得分:0)
马里奥看看下面的代码。我已经测试了它并且工作正常。
// Retrieving the address from address book...
ABAddressBookRef ref = ABAddressBookCreate();
CFArrayRef getArr = ABAddressBookCopyArrayOfAllPeople(ref);
CFIndex totCount = CFArrayGetCount(getArr);
for (int m = 0; m < totCount; m++)
{
ABRecordRef recordRef = CFArrayGetValueAtIndex(getArr, m);
ABMultiValueRef names = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
NSString* getFirstName = (__bridge NSString *)names;
NSLog(@"The name is :-%@\n", getFirstName);
getFirstName = (__bridge NSString*)ABMultiValueCopyValueAtIndex(names, 1);
NSLog(@"The phone number is :-%@\n", getFirstName);
}
如有任何疑虑,请回复我。