Monday, 22 May 2017

Get Contact Address Book

//
//  ContactVC.m
//  QuickBloxDemo
//

////  AddressBook framework Add and Add Plist file name.Privacy Contact description
//  Raman Mann



#import "ContactVC.h"
#import <AddressBook/AddressBook.h>
@interface ContactVC ()<UITableViewDelegate,UITableViewDataSource>
    {
        NSMutableArray * arrayContactData;
        NSMutableArray * nameArray;
        NSMutableArray *addressBookNumList;
        NSArray *sortedArr;
    }
@end
@implementation ContactVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    self.navigationItem.title = @"Contact";
    _tblvw.delegate =self;
    _tblvw.dataSource=self;
    addressBookNumList =[[NSMutableArray alloc]init];
    [self getContacts];
   
    NSLog(@"%@",[QBChat instance].contactList.contacts);
    NSLog(@"%@",[QBChat instance].contactList.pendingApproval);
   
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)getContacts
{
   
    arrayContactData =[[NSMutableArray alloc]init];
    // NSString * phoneStr ;
    NSString *firstName;
    nameArray =[[NSMutableArray alloc]init];
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
                                                 {
                                                    
                                                 });
    }
    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 );
           
            firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
            NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
           
            if (lastName==NULL)
            {
                lastName =@"";
            }
            NSString * name =[NSString stringWithFormat:@"%@ %@",firstName,lastName];
            //NSLog(@"%@",name);
           
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
            [[UIDevice currentDevice] name];
           
            for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++)
            {
                NSString * phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                //NSLog(@"%@",phoneNumber);
                // NSString * phoneNumDecimalsOnly = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
                if ([name isEqualToString:@"(null)"])
                {
                    name = phoneNumber;
                }
                [nameArray addObject:name];
               
                [addressBookNumList addObject:phoneNumber];
               
               
                NSMutableDictionary * contactDic = [[NSMutableDictionary alloc]init];
                [contactDic setObject:name forKey:@"name"];
                [contactDic setObject:phoneNumber forKey:@"phoneNumber"];
                [arrayContactData addObject:contactDic];
            }
           
        }
       
        NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        sortedArr = [arrayContactData sortedArrayUsingDescriptors:@[sd1]];
       
        //NSLog(@"%@",sortedArr);
       
        NSString *     str = [NSString stringWithFormat:@"%@",[addressBookNumList firstObject]];
       
        for (int iterator = 1; iterator < addressBookNumList.count; iterator++)
        {
            str = [NSString stringWithFormat:@"%@,%@",str,addressBookNumList[iterator]];
        }
    }
    else
    {
        // Send an alert telling user to change privacy setting in settings app
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return  sortedArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellid"] ;
   
   
    //[[arrayContactData valueForKey:@"name"]objectAtIndex:indexPath.row];
    cell.textLabel.text =[[sortedArr valueForKey:@"name"]objectAtIndex:indexPath.row];
   
    //NSLog(@"%@",[[sortedArr valueForKey:@"name"]objectAtIndex:indexPath.row]);
    NSLog(@"%@",sortedArr);
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f];
    cell.detailTextLabel.text = [[sortedArr valueForKey:@"phoneNumber"]objectAtIndex:indexPath.row];
   
    return cell;
}

@end



No comments:

Post a Comment