问题
With ABAddressBook, when I wanted the user to be able to have the options of "Create New Contact" and "Add to Existing Contact" for a contact they hadn't seen before, I would create and present an ABUnknownPersonViewController
.
I can find no way to replicate this functionality in the CNContacts framework. It seemed to me that CNContactViewController(forUnknownContact: contact)
could work, but unfortunately this only lets the user "Send Message" or "Share Contact."
How can I allow a user to save the contact to their address book, either as a new contact or as part of an existing one, in CNContacts?
func presentContact() {
let status = CNContactStore.authorizationStatusForEntityType(.Contacts)
switch status {
case .Authorized: ()
case .NotDetermined: requestAccess()
case .Denied, .Restricted: accessDenied()
}
print("authorized? \(status == .Authorized)") //prints "authorized? true"
let unknown = CNContactViewController(forUnknownContact: contact!)
unknown.delegate = self
self.navigationController?.pushViewController(unknown, animated: false)
}
Even when I try to request access, the user still can't save the contact.
回答1:
You keep not showing your real code, so it's impossible to help you. So I've lost interest. I'll just show you my real code instead and leave you to study it and think about the difference between what I'm doing and what you're doing. Here's actual working code; go ye and do likewise:
let con = CNMutableContact()
con.givenName = "Johnny"
con.familyName = "Appleseed"
con.phoneNumbers.append(CNLabeledValue(
label: "woods", value: CNPhoneNumber(stringValue: "555-123-4567")))
let unkvc = CNContactViewController(forUnknownContact: con)
unkvc.message = "He knows his trees"
unkvc.contactStore = CNContactStore()
unkvc.delegate = self
unkvc.allowsActions = false
self.navigationController?.pushViewController(unkvc, animated: true)
回答2:
CNContactStore *store = [[CNContactStore alloc] init];
CNMutableContact *contact = [[CNMutableContact alloc] init];
CNPhoneNumber * number = [[CNPhoneNumber alloc] initWithStringValue:@"1234567890"];
CNLabeledValue * labelValue = [[CNLabeledValue alloc]initWithLabel:CNLabelPhoneNumberMobile value:number];
NSMutableArray<CNLabeledValue *> *phoneNumbers = [NSMutableArray new];
[phoneNumbers addObject:labelValue];
contact.phoneNumbers = phoneNumbers;
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
controller.contactStore = store;
controller.delegate = self;
[self presentViewController:[[UINavigationController alloc] initWithRootViewController:controller] animated:YES completion:nil];
This code will work for "Create New Contact", for "Add to Existing Contact" we will have to use CNContactPickerViewController
CNContactPickerViewController * picker = [[CNContactPickerViewController alloc] init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
and the in the delegate method
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
CNContactStore *store = [[CNContactStore alloc] init];
CNMutableContact *existingContact = [(CNMutableContact*)contact mutableCopy];
CNPhoneNumber * number = [[CNPhoneNumber alloc] initWithStringValue:@"1234567890"];
CNLabeledValue * labelValue = [[CNLabeledValue alloc]initWithLabel:CNLabelPhoneNumberMobile value:number];
NSMutableArray<CNLabeledValue *> *phoneNumbers = [NSMutableArray new];
[phoneNumbers addObject:labelValue];
[phoneNumbers addObjectsFromArray:existingContact.phoneNumbers];
existingContact.phoneNumbers = phoneNumbers;
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:existingContact];
controller.contactStore = store;
controller.delegate = self;
dispatch_async(dispatch_get_main_queue(), ^
{
[self presentViewController:[[UINavigationController alloc] initWithRootViewController:controller] animated:YES completion:nil];
});
}
although it will show the Done button, instead of Update, but it will perform the functionality definitely as iPhone's default behaviour in Contacts App.
回答3:
What you're missing in your code is setting the contactStore
property of your unknown
variable to a handle of a CNContactStore
.
[...]
unknown.contactStore = CNContactStore()
[...]
来源:https://stackoverflow.com/questions/39192198/create-new-contact-and-add-to-existing-contact-for-cncontactviewcontroller