如何从iOS联系人导入邮件ID

时间:2016-02-03 07:07:45

标签: ios objective-c uisearchbar addressbook

我正在开发一个ios应用程序。我必须从设备联系人实现导入联系人(邮件ID),并且在执行搜索操作时必须在弹出窗口中显示它。如何才能完成..请帮忙

1 个答案:

答案 0 :(得分:1)

您可以使用CNContact获取电子邮件地址。请尝试使用此代码

//ios 9+
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES) {
        //keys with fetching properties
        NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey];
        NSString *containerId = store.defaultContainerIdentifier;
        NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
        NSError *error;
        NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
        if (error) {
            NSLog(@"error fetching contacts %@", error);
        } else {
            for (CNContact *contact in cnContacts) {
                // copy data to my custom Contacts class. 
                Contact *newContact = [[Contact alloc] init];
                newContact.firstName = contact.givenName;
                newContact.lastName = contact.familyName;
                UIImage *image = [UIImage imageWithData:contact.imageData];
                newContact.image = image;
                for (CNLabeledValue *label in contact.phoneNumbers) {
                    NSString *phone = [label.value stringValue];
                    if ([phone length] > 0) {
                        [contact.phones addObject:phone];
                    }
                }
            }
        }
    }        
}];

使用AddressBook请参阅此AddressBook

搜索这是演示代码

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

_firstname = [[NSMutableArray alloc]init];
_email = [[NSMutableArray alloc]init];
_lastname = [[NSMutableArray alloc]init];
_number = [[NSMutableArray alloc]init];
_fullname = [[NSMutableArray alloc]init];
_arrContacts = [[NSMutableArray alloc]init];
_newnumber = [[NSMutableArray alloc]init];
tabledatafirst = [[NSMutableArray alloc]init];
tabledatasecond = [[NSMutableArray alloc]init];
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {

        ABAddressBookRef addressBook = ABAddressBookCreate( );

    });

}

else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    NSMutableArray *abc = [[NSMutableArray alloc]init];

    CFErrorRef *error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

    for(int i = 0; i < numberOfPeople; i++) {



        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));

        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonEmailProperty));

        NSString *email = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonEmailProperty));


        if (firstName != nil) {
            [_firstname addObject:firstName];

        }
        else{
            [_firstname addObject:@"Not Found"];
        }
        if (lastName != nil) {
            [_lastname addObject:lastName];
        }
        else{
            [_lastname addObject:@"Not Found"];
        }
        //store email in array
        if (email !=nil) {
            [_email addObject:email];
        }
        else{
            [_email addObject:@"Not Found"];
        }
       //for search contacts use this 
       for (int i=0; i<_email.count; i++) {
    if([[_email objectAtIndex:i]isEqualToString:_searchtextfield.text]){
        NSLog(@"FIRST NAME %@",[_firstname objectAtIndex:i]);
        _addbuttonoutlet.enabled = YES;
        _firstnamelbl.text = [_firstname objectAtIndex:i];
        _secondnamelbl.text = [_lastname objectAtIndex:i];
        NSLog(@"LASTNAME %@",[_lastname objectAtIndex:i]);
        NSLog(@"FOUND ONCE");
        break;
    }
    else{
        _firstnamelbl.text = @"Sorry Contact NOT found";
        _secondnamelbl.text = @"";
        _addbuttonoutlet.enabled = NO;
    }
}