简体   繁体   English

iOS地址簿谓词

[英]iOS address book predicate

I am getting an array of all the contacts in address book as so: 我正在通讯簿中获取所有联系人的数组,如下所示:

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

What format would a predicate be in for say the first name of a contact? 谓词可以用哪种格式表示联系人的名字? I have tried record. 我已经尝试记录。 as suggested in this question: Search ABAddressbook iOS SDK but I get an unknown key exception. 如以下问题所建议: 搜索ABAddressbook iOS SDK,但出现未知的关键异常。 The items in the array seem to be __NSCFType type. 数组中的项目似乎是__NSCFType类型。 Any help would be greatly appreciated. 任何帮助将不胜感激。

hey for get contact info from device you can use my bellow code also with Contact bean class which i create just see this.. 嘿,要从设备获取联系信息,您也可以将我的波纹管代码与我创建的Contact bean类一起使用,只需看一下即可。

Also you need to include AddressBook.framework 另外,您还需要包含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);


}

and here MContact is NObject (Bean) file bellow 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

i hope this help you... 希望对您有帮助...

Mario have a look at below code. Mario看下面的代码。 I've tested it and it's working fine. 我已经对其进行了测试,并且工作正常。

    // 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);
    }

In any concern, please get back to me. 如有任何疑问,请与我联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM