【问题标题】:Using CNContactViewController without adding a contact (swift)在不添加联系人的情况下使用 CNContactViewController (swift)
【发布时间】:2016-12-21 07:50:23
【问题描述】:

我想使用 contactsUI CNContactViewController.init(forNewContact: a) 仅用于查看并将值提供给服务器。但是,现在这个功能也将联系人添加到地址簿/联系人应用程序中,是否可以防止这种情况发生?

【问题讨论】:

  • 您找到解决方案了吗?
  • @beseder 当我们使用contactViewController委托时,我们点击完成后立即获得CNcontact,所以我从该cncontact中获得标识符并删除它。使用 ForNewContact -> 完成 -> 将其数据发送到我们的服务器/应用程序 -> 以编程方式从 ios 联系人中删除联系人
  • 谢谢!这也是我同时结束的:-)但感觉很糟糕......最重要的是:它需要对用户联系人的写入权限,而我实际上并不需要。所以CNContactViewControllercontactStore 属性的行为不像记录的那样,对吧?
  • 您有没有找到更好的解决方案?

标签: ios swift uiviewcontroller swift3 cncontact


【解决方案1】:

联系人框架参考:https://developer.apple.com/library/ios/documentation/Contacts/Reference/Contacts_Framework/

示例: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

【讨论】:

  • 这个解决方案有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 2023-03-10
  • 2020-12-21
相关资源
最近更新 更多