我是iPhone编程新手 我想问一下,我是否可以添加,更新或删除iPhone联系人列表中的联系人。 请建议或提供一些有用的链接。
答案 0 :(得分:2)
答案 1 :(得分:0)
删除联系人:
- (void) deleteAllContacts {
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
NSArray *keys = @[CNContactPhoneNumbersKey];
NSString *containerId = contactStore.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
for (CNContact *contact in cnContacts) {
//Here write the necessary condition to filter the contacts which you want to delete .
[saveRequest deleteContact:[contact mutableCopy]];
}
[contactStore executeSaveRequest:saveRequest error:nil];
DDLogVerbose(@"Deleted contacts %lu", cnContacts.count);
}
}
}];
}
添加联系人:
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Access to contacts" message:@"This app requires access to contacts" preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"Go to settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:TRUE completion:nil];
return;
}
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
dispatch_async(dispatch_get_main_queue(), ^{
//user didn't grant access
});return;}
//create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
NSMutableString *temp = [[NSMutableString alloc] initWithString:newContactName];
[temp appendString:@"(PATIENT)"];
NSString *immutable = [NSString stringWithString:temp];
contact.givenName = immutable;
CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:mobileNumberOfPatient]];
contact.phoneNumbers = @[homePhone];
CNSaveRequest *request = [[CNSaveRequest alloc] init];
[request addContact:contact toContainerWithIdentifier:nil];
[self.activityIndicator setHidden:YES];
[self.activityIndicator stopAnimating];
[self shareViaWhatsApp];
//save it
NSError *saveError;
if (![store executeSaveRequest:request error:&saveError]) {
NSLog(@"error: %@",saveError);
}
}];