Swift 3 add new contact with phone and email information

后端 未结 3 1029
天涯浪人
天涯浪人 2021-02-13 06:48

I\'m trying to prompt the user to create a new contact and pass in information. (specifically a phone and email)

I\'ve found numerous examples of using a CNMutableContac

相关标签:
3条回答
  • 2021-02-13 07:13

    Swift 4

    import ContactsUI
    

    implement delegate CNContactViewControllerDelegate

    @IBAction func UserTap_Handler(_ sender: Any) {
    
            self.navigationController?.isNavigationBarHidden = false
            let con = CNContact()
            let vc = CNContactViewController(forNewContact: con)
            vc.delegate = self
            _ = self.navigationController?.pushViewController(vc, animated: true)
        }
    
        //MARK:- contacts delegates
        func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
            print("dismiss contact")
            self.navigationController?.popViewController(animated: true)
        }
        func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
            return true
        }
    
    0 讨论(0)
  • 2021-02-13 07:35

    You Can Do Something Like This.

    extension ViewController: CNContactViewControllerDelegate {
    
        func showNewContactViewController() {
    
            let contactViewController: CNContactViewController = CNContactViewController(forNewContact: nil)
            contactViewController.contactStore = CNContactStore()
            contactViewController.delegate = self
            let navigationController: UINavigationController = UINavigationController(rootViewController: contactViewController)
            present(navigationController, animated: false) {
                print("Present")
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-13 07:39
    import ContactsUI
    
    //add CNContactViewControllerDelegate to your ViewController
    class ViewController: UIViewController , CNContactViewControllerDelegate {
    
    func addPhoneNumber(phNo : String) {
      if #available(iOS 9.0, *) {
          let store = CNContactStore()
          let contact = CNMutableContact()
          let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue :phNo ))
          contact.phoneNumbers = [homePhone]
          let controller = CNContactViewController(forUnknownContact : contact)
          controller.contactStore = store
          controller.delegate = self
          self.navigationController?.setNavigationBarHidden(false, animated: true)
          self.navigationController!.pushViewController(controller, animated: true)
      }
    }
    
    0 讨论(0)
提交回复
热议问题