我想检查iphone电话簿是否已经添加了联系人,如果已添加,那么我将不会添加它,它不添加然后我将添加新的联系人,,,, 我想只想比较电话簿中存在的“Veer”等名称
答案 0 :(得分:3)
ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(Nil, Nil);
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressbook);
NSMutableArray *namearray=[[NSMutableArray alloc] init];
for(id person in people){
NSString *firstNameString = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(person),kABPersonFirstNameProperty);
NSLog(@"%@",firstNameString);
[namearray addObject:firstNameString];
}
NSLog(@"%@",namearray);
NSString *yourString = name you want to add;
BOOL identicalStringFound = NO;
for (NSString *someString in namearray) {
if ([someString isEqual:yourString]) {
identicalStringFound = YES;
break;
}
}
if(identicalStringFound)
{
//code you want to perform
}
else
{
//code you want to perform
}
答案 1 :(得分:1)
+ (NSArray *) findContactsContainingName: (NSString *) fname
{
// Load the contacts
ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *contacts = [NSMutableArray arrayWithCapacity:thePeople.count];
for (id person in thePeople)
[array addObject:[ABContact contactWithRecord:(ABRecordRef)person]];
[thePeople release];
// search the contacts for fname
NSPredicate *pred;
pred = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@ OR lastname contains[cd] %@ OR nickname contains[cd] %@ OR middlename contains[cd] %@", fname, fname, fname, fname];
return [contacts filteredArrayUsingPredicate:pred];
}