I Want to Delete Duplicate or Merge duplicate Contact Can anyOne Provide me Sample Code for it !!!!! I want to get the List of Duplicate Contact in tableview and merge them or delete them
Asked
Active
Viewed 1,404 times
0
-
How to fetch contact list ? Through ABAddressBook ? – Abha Oct 13 '16 at 09:55
-
i fetched the complete contact list but know i want to get duplicate contact and merge them...!! allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); – Mitesh Varu Oct 13 '16 at 10:12
-
Than what issue you face by using this allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); – Abha Oct 13 '16 at 10:40
-
Check my answer – user3182143 Oct 13 '16 at 10:49
-
i m getting all contacts no issue but now i want sync all the contact and find duplicate contact and merge them and delete another contact.... – Mitesh Varu Oct 13 '16 at 10:50
-
2@user3182143 thank you brother let me try it.... if it works for me and can you tell me how to merge this contact with the existing contacts – Mitesh Varu Oct 13 '16 at 10:55
-
My code works perfectly brother.I deleted the duplicate contact as you ask. – user3182143 Oct 13 '16 at 11:00
-
I did what you ask in your question " I Want to Delete Duplicate ". – user3182143 Oct 13 '16 at 11:03
-
thank you brother@user3182143 but its not deleting form the address book contact is available in contact – Mitesh Varu Oct 13 '16 at 11:09
-
can duplicate contact merge with each other and can you tell me how to compare phone number and remove duplicates – Mitesh Varu Oct 13 '16 at 11:33
-
Not addressbook contact.now it is contact.We don't have concept addressbook in iOS 9 onwards. – user3182143 Oct 13 '16 at 12:06
-
but still in the contact list of my iPhone all the contact are shown including all duplicate contact here i want to merge the duplicate contact and delete one off the duplicate contacts. – Mitesh Varu Oct 14 '16 at 04:40
-
@MiteshVaru Have you done it? Because I also want to do same but couldn't do it. If you have done it successfully please help me here!! – dmaulikr Aug 09 '18 at 11:27
1 Answers
5
I created sample project for you.I got the solution.it works perfectly.
Addressbook framework is deprecated from iOS 9.So we need to use Contact framework.
We must import Contact framework
Next in plist If you want to access contacts,you have to get authorization permission so you need to add Privacy - Contacts Usage Description
Key is Privacy - Contacts Usage Description
Type is string
Value is contact (whatever you want add here as string)
ViewController.h
#import <UIKit/UIKit.h>
#import <Contacts/Contacts.h>
@interface ViewController : UIViewController<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tblViewContact;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arrData;
NSMutableArray *arraySearchContactData;
}
@end
@implementation ViewController
@synthesize tblViewContact;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrData = [[NSMutableArray alloc]init];
[self getContact];
[tblViewContact registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
//Get Contact and Authorization Access
-(void)getContact
{
// Request authorization to Contacts
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
{
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
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;
NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
NSMutableArray *arrContacts = [[NSMutableArray alloc]init];
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:@"person-icon.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];
[arrContacts addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]];
}
//Removing Duplicate Contacts from array
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:arrContacts];
NSArray *arrayWithoutDuplicates = [orderedSet array];
arrData = [arrayWithoutDuplicates mutableCopy];
NSLog(@"The contacts are - %@",arrData);
dispatch_async(dispatch_get_main_queue(), ^{
[tblViewContact reloadData];
});
}
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableView Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
cell.textLabel.text = arrData[indexPath.row];
return cell;
}
The Printed results For Contacts
The contacts are - (
"John Appleseed",
"Kate Bell",
"Anna Haro",
"Daniel Higgins",
"David Taylor",
"Hank Zakroff"
)
Screenshot below
When first you run the app
Now it shows contacts in table view

user3182143
- 9,459
- 3
- 32
- 39