是否可以检查联系人是否退出 iPhone 的联系人列表?
Posted
技术标签:
【中文标题】是否可以检查联系人是否退出 iPhone 的联系人列表?【英文标题】:Is it possible to check a contact whether it is exits in contact list of iPhone or not? 【发布时间】:2014-06-11 10:35:23 【问题描述】:我正在开发一个应用程序,我必须将联系人保存在通讯录中,但如果联系人已经保存在联系人中,则不应保存。
但我不知道
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
我认为您需要先获取所有联系人,然后需要在该联系人中找到一个号码。检查这个***.com/questions/19027118/fetch-contacts-in-ios-7 这是一个很好的tutorial 用于获取手机的联系人列表,然后使用它你可以检查是否添加.. 为什么投反对票?我找到了路。 【参考方案1】:已解决问题
-(void)CheckContactIsExits
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
ABRecordRef pet = ABPersonCreate();
ABRecordSetValue(pet, kABPersonFirstNameProperty, (__bridge CFStringRef)@"VoxSci Activation", nil);
for (id record in allContacts)
ABRecordRef thisContact = (__bridge ABRecordRef)record;
if (CFStringCompare(ABRecordCopyCompositeName(thisContact),
ABRecordCopyCompositeName(pet), 0) == kCFCompareEqualTo)
NSLog(@"The contact already exists");
//The contact already exists!
isContactExits=YES;
【讨论】:
它在行崩溃(CFStringCompare(ABRecordCopyCompositeName(thisContact),出现 EXC_BAD_ACCESS 错误..请您帮忙【参考方案2】:假设您为此使用 Apples 框架,您的用户将可以选择“创建新联系人”或者如果它已经在联系人列表中“添加到现有联系人”。因此用户可以决定是否应该添加它
【讨论】:
【参考方案3】:#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
并读取iPhone中的所有联系人并检查姓名或电话是否退出。
【讨论】:
【参考方案4】:通过联系电话检查联系人是否退出
注意:仅将 checkingPhoneNumber 变量替换为您的检查联系人号码
ABAddressBookRef * addressbook = ABAddressBookCreateWithOptions(Nil, Nil);
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressbook);
NSMutableArray *phoneArray=[[NSMutableArray alloc] init];
for(id person in people)
// get person contact number
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
NSString* mobile=@"";
NSString* mobileLabel;
for (int i=0; i < ABMultiValueGetCount(phones); i++)
mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
NSLog(@"mobile:");
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
NSLog(@"iphone:");
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel])
NSLog(@"pager:");
mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@", mobile);
// remove all spaces bracket from contact number
NSMutableString *newPhoneStr = [[NSMutableString alloc] init];;
int j = [mobile length];
for (int i=0; i<j; i++)
if ([mobile characterAtIndex:i] >=48 && [mobile characterAtIndex:i] <=59)
[newPhoneStr appendFormat:@"%c",[mobile characterAtIndex:i]];
//add contact into phoneArray
[phoneArray addObject:newPhoneStr];
NSLog(@"%@",phoneArray);
BOOL identicalStringFound = NO;
// remove all spaces bracket from contact number which is check
NSMutableString *newCheckingPhoneNumberStr = [[NSMutableString alloc] init];
int j = [checkingPhoneNumber length];
for (int i=0; i<j; i++)
if ([checkingPhoneNumber characterAtIndex:i] >=48 && [[profileDetailsDict valueForKey:@"mobile"] characterAtIndex:i] <=59)
[newCheckingPhoneNumberStr appendFormat:@"%c",[checkingPhoneNumber characterAtIndex:i]];
for (NSString *contact in phoneArray)
if ([contact isEqual:newCheckingPhoneNumberStr])
identicalStringFound = YES;
break;
if(identicalStringFound)
// checkingPhoneNumber is exit
else
// checkingPhoneNumber is not exit
【讨论】:
【参考方案5】:如果有人想检查姓名是否保存在通讯录中,那么下面的功能可能会有用:
-(BOOL)isNameSaved:(NSString*)strGivenName
BOOL isSaved = NO;
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for ( int i = 0; i < nPeople; i++ )
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstc = (CFStringRef)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *first = [NSString stringWithFormat:@"%@",firstc];
if ([first isEqualToString:strGivenName])
isSaved = YES;
break;
return isSaved;
【讨论】:
以上是关于是否可以检查联系人是否退出 iPhone 的联系人列表?的主要内容,如果未能解决你的问题,请参考以下文章