1

我得到了地址簿中所有联系人的数组:

  NSMutableArray *records = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople( addressBook );

谓词以什么格式表示联系人的名字?我试过记录。正如这个问题中所建议的那样:搜索 ABAddressbook iOS SDK但我得到一个未知的密钥异常。数组中的项目似乎是__NSCFType类型。任何帮助将不胜感激。

4

2 回答 2

1

嘿,从设备获取联系信息,您可以使用我的波纹管代码也与我创建的联系 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

我希望这可以帮助你...

于 2012-11-29T12:16:32.983 回答
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);
    }

如有任何疑问,请回复我。

于 2012-11-29T12:37:57.193 回答