Yat*_*dra 0 iphone objective-c ios cncontact
我正在尝试在我的联系人中导入 vcf 文件。当我第一次保存成功导入的联系人时,没问题。但是当我尝试更新同一个联系人时,它什么也不做。我想要什么?我想如果我将更新联系人然后电话号码添加到现有联系人中。我正在使用 CNContact。
保存联系人:
-(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);
}
Run Code Online (Sandbox Code Playgroud)
更新
-(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);
}
Run Code Online (Sandbox Code Playgroud)
您可以只用姓名替换您的号码
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1670 次 |
| 最近记录: |