1

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

Sanket_B
  • 735
  • 9
  • 26
viratpuar
  • 524
  • 1
  • 5
  • 22
  • Version Note: `ABAddressBook.framework` is deprecated since iOS 9; use `Contacts.framework` instead if you want to target iOS 9 or above only. – Raptor Feb 02 '16 at 02:23

1 Answers1

1

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

Vijay Palwe
  • 109
  • 10
  • I store it in NSMutable Dictionary i am getting null value. please provide demo code for it. – viratpuar Feb 01 '16 at 12:03
  • 2
    dnt take gloabl dictionary create local object where u add the name and all in for(int i = 0; i < numberOfPeople; i++) { nsdictionary *dict = [nsdictionary alloc ]init] add the name lastname and number in dictonary and add dicatioany to array in loof itself – Vijay Palwe Feb 01 '16 at 12:27