ios - 在不添加联系人的情况下使用 CNContactViewController (swift)

标签 ios swift uiviewcontroller swift3 cncontact

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

最佳答案

联系人框架引用: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

关于ios - 在不添加联系人的情况下使用 CNContactViewController (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38949454/

相关文章:

ios - 从 ALAsset (Video) iOS Video 获取最后修改日期

swift - 如何使用 swift 4 测试 iPhone 与互联网的连接?

Swift:泛型和类型约束,奇怪的行为

objective-c - 呈现强制横向的模态视图 Controller

ios - 执行 Push Segue 时是否应该释放 UIViewController

ios - Xcode 5 Storyboard 创建的 UIViewController 布局在以编程方式创建新 VC 时未被调用

ios - 居中 UILabel 不起作用?

ios - 如何识别目标 View Controller 上的 segue 标识符?

javascript - iOS 8 用户代理上的 Chrome 不再包含 crIOS

iOS Swift 可解码 : Error: Cannot invoke initializer for type with no arguments