xcode Swift 3 添加带有电话和电子邮件信息的新联系人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41755969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:44:08  来源:igfitidea点击:

Swift 3 add new contact with phone and email information

iosswiftxcodeswift3contacts

提问by TheMooCows237

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 CNMutableContact and adding an email to it. However, any of the code involving the CNContact gives me a "Use of undeclared type" error.

我发现了许多使用 CNMutableContact 并向其添加电子邮件的示例。但是,任何涉及 CNContact 的代码都会给我一个“使用未声明的类型”错误。

How can I setup my class to prompt the user to save the contact?

如何设置我的课程以提示用户保存联系人?

回答by Ganesh Kumar

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)
  }
}

回答by Satish Babariya

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")
        }
    }
}

回答by Azhar

Swift 4

斯威夫特 4

import ContactsUI

implement delegate CNContactViewControllerDelegate

实现委托 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
    }