I am trying to import vcf file in my contacts.When First time I save a contact it imported successfully, no problem.But when I am trying to update that same contact it's doing nothing.What I want? I want if I will update contact then phone numbers add in exist contact.I am using CNContact.
to save contacts:
-(void)saveVCardContacts:(CNContact *)contact{
NSError * error;
CNSaveRequest *saveRequest = [[CNSaveRequest alloc]init];
[saveRequest addContact:[contact mutableCopy] toContainerWithIdentifier:nil];
BOOL success = [self.store executeSaveRequest:saveRequest error:&error];
if(success)
NSLog(@"import successfully");
else
NSLog(@"Error = %@",error);
}
to update
-(void)updateVCardContacts:(CNContact *)contact{
NSError *error;
CNSaveRequest *saveRequest = [[CNSaveRequest alloc]init];
[saveRequest updateContact:[contact mutableCopy]];
BOOL success = [self.store executeSaveRequest:saveRequest error:&error];
if(success)
NSLog(@"update successfully");
else
NSLog(@"Error = %@",error);
}
You can replace your number with only names
CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
CNContactStore * store = [[CNContactStore alloc]init];
NSArray* arrFetchedcontact = nil;
@try {
NSError * err = nil;
NSArray * keytoFetch = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey];
NSPredicate * predicate = [CNContact predicateForContactsMatchingName:GivenNames];
arrFetchedcontact = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keytoFetch error:&err];
}
@catch (NSException *exception) {
NSLog(@"description = %@",[exception description]);
}
if([arrFetchedcontact count] > 0)
{
NSLog(@"ArrFetchedContact %@",arrFetchedcontact);
CNMutableContact * contactToUpdate = [[arrFetchedcontact objectAtIndex:0] mutableCopy];
NSMutableArray * arrNumbers = [[contactToUpdate phoneNumbers] mutableCopy];
[arrNumbers removeObjectAtIndex:0];
CNLabeledValue * homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:FieldNumbers]];
NSLog(@"Print Homephone %@",homePhone);
[arrNumbers addObject:homePhone];
[contactToUpdate setPhoneNumbers:arrNumbers];
[saveRequest updateContact:contactToUpdate];
@try {
NSLog(@"Success %d",[store executeSaveRequest:saveRequest error:nil]);
}
@catch (NSException *exception) {
NSLog(@"description = %@",[exception description]);
}
}