Saturday, 17 June 2017

// Contact BOOK with CNContacts

//  ContactVc.m
//
//Add two framework Contacts.framwork and ContactsUI.framwork
//  Add InfoPlist ===> Privacy - Contacts Usage Description
//          create the outlet of tableView ===>  @property (strong, nonatomic) IBOutlet UITableView *tableVw;


#import "ContactVc.h"
@import Contacts;
#import <Contacts/Contacts.h>
@interface ContactVc ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_contacts;
    NSMutableArray *contactsArray;
   
}

@end

@implementation ContactVc

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableVw.delegate=self;
    _tableVw.dataSource=self;
//    NameArr =[[NSMutableArray alloc]init];
//    phNo =[[NSMutableArray alloc]init];
//    imageArr =[[NSMutableArray alloc]init];
    [self contactsDetailsFromAddressBookCNContacts];
   // [self contactList];
   // [self getPhList];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return contactsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[_tableVw dequeueReusableCellWithIdentifier:@"cellid"];
    UIImageView *imagVw=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 44, 44)];
    imagVw.layer.cornerRadius=imagVw.frame.size.height/2;
    imagVw.image=[[contactsArray valueForKey:@"userImage"]objectAtIndex:indexPath.row];
    [cell addSubview:imagVw];
    UILabel *lblName =[[UILabel alloc]initWithFrame:CGRectMake(imagVw.frame.size.width+20, 0, cell.frame.size.width-imagVw.frame.size.width, 22)];
    
    lblName.text=[[contactsArray valueForKey:@"fullName"]objectAtIndex:indexPath.row];
    UILabel *lblPhNo =[[UILabel alloc]initWithFrame:CGRectMake(imagVw.frame.size.width+20, 25, cell.frame.size.width-imagVw.frame.size.width, 18)];
   lblPhNo.text= [[contactsArray valueForKey:@"PhoneNumbers"]objectAtIndex:indexPath.row];
    [cell addSubview:lblName];
    [cell addSubview:lblPhNo];
    cell.backgroundColor=[UIColor colorWithRed:0.91 green:1.00 blue:1.00 alpha:1.0];
    //cell.imageView.image=[imageArr objectAtIndex:indexPath.row];
       // cell.textLabel.text=[NameArr objectAtIndex:indexPath.row];
       // cell.detailTextLabel.text=[phNo objectAtIndex:indexPath.row];
    return cell;
}
-(void)contactsDetailsFromAddressBookCNContacts{
    //ios 9+
    contactsArray =[[NSMutableArray alloc]init];
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactBirthdayKey,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 {
                NSString *phone;
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                UIImage *profileImage;
                NSDateComponents *birthDayComponent;
                NSMutableArray *contactNumbersArray;
                NSString *birthDayStr;
                NSMutableArray *emailArray;
                NSString* email = @"";
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;
               
                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    UIImage *image = [UIImage imageWithData:contact.imageData];
                    if (image != nil) {
                        profileImage = image;
                    }else{
                        profileImage = [UIImage imageNamed:@"placeholder.png"];
                    }
                    for (CNLabeledValue *label in contact.phoneNumbers) {
                        phone = [label.value stringValue];
                        if ([phone length] > 0) {
                            [contactNumbersArray addObject:phone];
                        }
                    }
            NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                    NSLog(@"ResponseDict: %@",personDict);
                    [contactsArray addObject:personDict];
                    NSLog(@"contactsArray%@", contactsArray);
                   
                
                  [self.tableVw reloadData];
                    
                }

            }
        }
    }];
}
-(BOOL)shouldAutorotate{ return NO; }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end

No comments:

Post a Comment