繁体   English   中英

通过电话,电子邮件,姓名在AddressBook中找到ios ABRecordRef联系人

[英]find ios ABRecordRef contact in AddressBook by phone, email, name

我需要在AdreesBook中找到一个联系人,以添加新的社交网络。 有时,我必须通过其电话和电子邮件或其电话(firstName和lastName)来查找联系人,是否有某种查询来获取ABRecordRef联系人,而不是用whiles?

我的许多用户都有1000多个联系人,并且我需要更新其中的许多联系人,因此如果我唯一的解决方案是花很多时间,那效率就不高。

任何想法??

谢谢!

以下是可以帮助您使用电话号码获取详细联系方式的方法。 为此,它使用了kABPersonPhoneProperty ,同样可以编写另一个用于搜索电子邮件姓名的功能

  • 对于电子邮件 ,请使用以下属性: kABPersonEmailProperty
  • 对于名字kABPersonFirstNameProperty

有关更多详细信息,请访问: ABPerson参考

希望这可以帮助。

#import <AddressBook/AddressBook.h>

-(NSArray *)contactsContainingPhoneNumber:(NSString *)phoneNumber {
    /*
     Returns an array of contacts that contain the phone number
     */

    // Remove non numeric characters from the phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Create a new address book object with data from the Address Book database
    CFErrorRef error = nil;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (!addressBook) {
        return [NSArray array];
    } else if (error) {
        CFRelease(addressBook);
        return [NSArray array];
    }

    // Requests access to address book data from the user
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {});

    // Build a predicate that searches for contacts that contain the phone number
    NSPredicate *predicate = [NSPredicate predicateWithBlock: ^(id record, NSDictionary *bindings) {
        ABMultiValueRef phoneNumbers = ABRecordCopyValue( (__bridge ABRecordRef)record, kABPersonPhoneProperty);
        BOOL result = NO;
        for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            NSString *contactPhoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
            contactPhoneNumber = [[contactPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];
            if ([contactPhoneNumber rangeOfString:phoneNumber].location != NSNotFound) {
                result = YES;
                break;
            }
        }
        CFRelease(phoneNumbers);
        return result;
    }];

    // Search the users contacts for contacts that contain the phone number
    NSArray *allPeople = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    NSArray *filteredContacts = [allPeople filteredArrayUsingPredicate:predicate];
    CFRelease(addressBook);

    return filteredContacts;
}

暂无
暂无

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

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