简体   繁体   中英

how to search in address book using phone number ios

I am new in IOS Programming. In my simple app I am storing first name,last name and phone number in NSMutableArray.I have a UITextfield where I get the phone number.I wanna search first name and last name according to mobile number.I am storing firstname, lastname and phone number in different Mutable arrays so how to search firstname and lastname in stored array. This is my code

     ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

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

        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {                
            ABAddressBookRef addressBook = ABAddressBookCreate( );                
        });            
    }

    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        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));                
            if (firstName != nil) {
                [_firstname addObject:firstName];    
            }
            else{
                [_firstname addObject:@"Not Found"];
            }
            if (lastName != nil) {
                [_lastname addObject:lastName];
            }
            else{
                [_lastname addObject:@"Not Found"];
            }


            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);                
            [[UIDevice currentDevice] name];                
            NSArray *numbers = (__bridge NSArray *)(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));                
            if (numbers != nil) {                    
                [_arrContacts addObject:numbers];                    
                NSLog(@"Numbers:%@",_arrContacts); 
            }




            for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {                    
                NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i); 
                _addressBookNum = [_addressBookNum stringByAppendingFormat: @":%@",phoneNumber];                    
            }                
        }

        // NSLog(@"AllNumber:%@",_addressBookNum);

    }

    else {            
        // Send an alert telling user to change privacy setting in settings app            
    }

    CFRelease(addressBookRef);
    NSLog(@"This is Name: %@",_firstname);
    NSLog(@"This is LastName: %@",_lastname);
    NSLog(@"Number: %@",_arrContacts);    
}

Please suggest some idea to do this

Instead of saving name last name and number in different array store it in single array with dictionary. Suppose NSMutabelArray *details create dictionary having three values name lastname and number, and add this dictoianry into array, so it will be easy for you to further use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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