首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不添加联系人的情况下使用CNContactViewController (迅速)

在不添加联系人的情况下使用CNContactViewController (迅速)
EN

Stack Overflow用户
提问于 2016-08-15 04:48:09
回答 1查看 1.8K关注 0票数 4

我只想使用contactsUI CNContactViewController.init(forNewContact: a)作为视图,并将值传递给服务器。然而,现在func还添加了一个联系人到地址簿/联系人应用程序,它有可能防止这种情况吗?

EN

回答 1

Stack Overflow用户

发布于 2016-08-15 16:35:39

联系人框架参考:Framework/

样本: ViewController.swift

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38949454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档