Search code examples
iosobjective-ciphoneaddressbookabaddressbook

UIAddressBook in UITableView with NSObjectClass


I need to make a UITableView which fetches address book contents in it and also a UISearchBar which searches the address book contents.

I need an implementation like this project

http://www.appcoda.com/search-bar-tutorial-ios7/

But the problem is here the data is static and I want to load address book's data in it.


Solution

  • I have created ContactData NSObject Class. In following method, I am creating its object for each contact. At the end, you will get NSMutableArray named contactArr.

    -(void)loadContacts{

    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
            //NSLog(@"%@", addressBook);
        });
    }
    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));
    
            ContactData *dataObj = [[ContactData alloc]init];
            NSString *Name = @" ";
    
            if ([firstName length]>0) {
    
                if ([lastName length]>0) {
    
                    Name = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }
                else{
                    Name = firstName;
                }
            }
            else{
                Name = @" ";
            }
    
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            [[UIDevice currentDevice] name];
            //NSLog(@"\n%@\n", [[UIDevice currentDevice] name]);
    
            NSString *phoneNumber = @" ";
    
            for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
                phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
            }
    
            ABMultiValueRef Emails = ABRecordCopyValue(person, kABPersonEmailProperty);
            [[UIDevice currentDevice] name];
    
            NSString *Email; //= @" ";
    
            for (CFIndex i = 0; i < ABMultiValueGetCount(Emails); i++) {
                Email = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Emails, i);
    
            }
    
            ABMultiValueRef Addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
    
            NSString *address = @" ";
            NSMutableDictionary* addressDict = [[NSMutableDictionary alloc]init];
    
            for (CFIndex i = 0; i < ABMultiValueGetCount(Addresses); i++) {
                addressDict = (__bridge_transfer NSMutableDictionary *) ABMultiValueCopyValueAtIndex(Addresses, i);
    
                //NSLog(@"address = %@", addressDict);
    
                NSString * street = [addressDict objectForKey:@"Street"];
                NSString * city = [addressDict objectForKey:@"City"];
                NSString * state = [addressDict objectForKey:@"State"];
                NSString *country = [addressDict objectForKey:@"Country"];
    
    
                if (country.length>0 || state.length>0 || city.length>0 || street.length>0) {
    
                    if (country.length>0 && state.length>0) {
    
                        address = [NSString stringWithFormat:@"%@, %@", state,country];
                    }
                    else if(country.length>0 && city.length>0){
                        address = [NSString stringWithFormat:@"%@, %@", city,country];
                    }
                    else if (state.length>0 && street.length>0){
                        address = [NSString stringWithFormat:@"%@, %@", street,country];
                    }
                    else if (state.length>0 && city.length>0){
                        address = [NSString stringWithFormat:@"%@, %@", city,state];
                    }
                    else if (state.length>0 && street.length>0){
                        address = [NSString stringWithFormat:@"%@, %@", street,state];
                    }
                    else if (city.length>0 && street.length>0){
                        address = [NSString stringWithFormat:@"%@, %@", street,city];
                    }
                    else if (country.length>0){
                        address = country;
                    }
                    else if (state.length>0){
                        address = state;
                    }
                    else if (city.length>0){
                        address = city;
                    }
                }
                else{
                    address = @" ";
                }
            }
    
            //NSLog(@"address = %@", address);
    
            NSData  *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
    
            dataObj.name = Name;
            dataObj.email = Email;
            dataObj.phone = phoneNumber;
            dataObj.imageData = imgData;
            dataObj.address = address;
    
            [contactArr addObject:dataObj];
    
        }
        NSLog(@"contacts loaded");
        //NSLog(@"count = %lu", (unsigned long)contactArr.count);
    }
    else {
    
        //Go to settings and allow access to use contacts[GlobalFunction removeWaitView];
    
    }
    

    }