4

I want to use contactsUI CNContactViewController.init(forNewContact: a) for view only and take the value to the server. However, right now the func also adding a contact to addressbook/contact app, is it possible to prevent this?

Aldo Lazuardi
  • 1,898
  • 2
  • 19
  • 34
  • Did you find a solution for this? – beseder Feb 11 '17 at 09:43
  • @beseder When we use contactViewController delegate, we get the CNcontact right after we click done, so I get the identifier from that cncontact and delele it. Use ForNewContact -> done -> send its data to our server/app -> delete the contact from ios contact programmatically – Aldo Lazuardi Feb 13 '17 at 04:49
  • Thanks! That is what I also ended with meanwhile :-) But it feels bad ... And most of all: It needs write access to the user's contacts, which I in fact do NOT need. So the `contactStore` property of `CNContactViewController` does not behave as documented, right? – beseder Feb 13 '17 at 11:51
  • Did you at any point found a better solution? – Gertjan.com Jan 22 '19 at 10:43

1 Answers1

0

Contacts Framework Reference: 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
    }

}
}

download project files: https://www.dropbox.com/s/yqdhqld0osp508b/Archive.zip?dl=0

Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127