Adding and saving a new contact in AddressBook

I have a problem that I cannot solve, although I used the resources that I found on Google and here. I just started learning Swift and how to use Xcode about a month ago, therefore, I am very new, and the problem I have is probably very simple for others.

What I'm trying to do is add and save a new contact in the address book. I can switch from application to Contacts application without problems, I just can’t save new contact information.

import AddressBookUI import AddressBook class ViewController: UIViewController { @IBOutlet weak var contactLink: UIButton! @IBAction func contactLink(sender: AnyObject) { var viewController: ABNewPersonViewController = ABNewPersonViewController() self.presentViewController(viewController, animated: true, completion: nil) } } 

Here the code I use relates to my problem. Any help would be greatly appreciated.

+6
source share
1 answer

You are currently using the ContactsUI infrastructure. So in Swift 3 you can do:

 import ContactsUI class ViewController: UIViewController, CNContactViewControllerDelegate { @IBAction func contactLink(_ sender: AnyObject) { let controller = CNContactViewController(forNewContact: nil) controller.delegate = self let navigationController = UINavigationController(rootViewController: controller) self.present(navigationController, animated: true) } func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) { viewController.navigationController?.dismiss(animated: true) } } 

My initial answer using the AddressBookUI framework in Swift 2, below.


Swift Code:

 import AddressBookUI class ViewController: UIViewController, ABNewPersonViewControllerDelegate { @IBAction func contactLink(sender: AnyObject) { let controller = ABNewPersonViewController() controller.newPersonViewDelegate = self let navigationController = UINavigationController(rootViewController: controller) self.presentViewController(navigationController, animated: true, completion: nil) } func newPersonViewController(newPersonView: ABNewPersonViewController!, didCompleteWithNewPerson person: ABRecord!) { newPersonView.navigationController?.dismissViewControllerAnimated(true, completion: nil); } } 

See the “User Request to Create a New Entity Record” section in “Address Book Programming Guide: User Interaction: Requesting and Displaying Data .

+5
source

Source: https://habr.com/ru/post/977444/


All Articles