ios – 使用CNContactViewController而不添加联系人(swift)

我想使用contactsUI CNContactViewController.init(forNewContact:a)仅用于查看并将值带到服务器.但是,现在func还在地址簿/联系人应用程序中添加联系人,是否可以防止这种情况? 最佳答案 Contacts Framework参考:
https://developer.apple.com/library/ios/documentation/Contacts/Reference/Contacts_Framework/

sample: ViewController.swift

import UIKit
import ContactsUI

class ViewController: UIViewController, CNContactPickerDelegate {

let contactPickerViewController = CNContactPickerViewController()
@IBOutlet var titleLabel: UILabel!
@IBOutlet var valueLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    contactPickerViewController.delegate = self
    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.
}

@IBAction func touchUpInside(sender: UIButton) {
    self.presentViewController(contactPickerViewController, animated: true, completion: nil)
}

func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {

    titleLabel.text = ""
    valueLabel.text = ""

    if let label = contactProperty.label {
        titleLabel.text = CNLabeledValue.localizedStringForLabel(label)
    }

    if let phone = contactProperty.value as? CNPhoneNumber {
        valueLabel.text = phone.stringValue
    }

    if let stringData = contactProperty.value as? String {
        valueLabel.text = stringData
    }

    if let adress = contactProperty.value as? CNPostalAddress {
        let adressString = adress.street + " " + adress.city + " " + adress.country + " " + adress.postalCode
        valueLabel.text = adressString
    }

}
}

下载项目文件:https://www.dropbox.com/s/yqdhqld0osp508b/Archive.zip?dl=0

点赞