从 iOS 中的联系方式获取街道、城市、州、邮政编码和国家/地区并填充到标签
Getting street, city, state, zip and country from contact details in iOS and populating to a label
我正在 Xcode 8 和 swift 3 中编写一个应用程序,我想在其中提取街道、城市、州、国家和邮政编码的邮政地址详细信息,并将它们填充到视图控制器上的标签。我已经修改了允许授权的 info.plist 文件,并且我编写了以下代码 below.I 创建了一个标签来保存信息以及一个用于访问联系人的按钮。
我能够获得填充到标签中的联系人的全名和电子邮件地址,但我不知道如何从邮寄地址。我已在 Stack Overflow 和互联网上进行了穷尽搜索,但无济于事。谨请求您的帮助。
欢迎提出任何见解和建议。
import UIKit
import Contacts
import ContactsUI
class ThirdViewController: UIViewController, CNContactPickerDelegate {
@IBOutlet weak var lblDetails: UILabel!
@IBOutlet weak var backTextField: UITextView!
@IBAction func btnSelectContact(_ sender: Any) {
let entityType = CNEntityType.contacts
let authStatus = CNContactStore.authorizationStatus(for:entityType)
if authStatus == CNAuthorizationStatus.notDetermined {
let contactStore = CNContactStore.init()
contactStore.requestAccess(for: entityType, completionHandler: {(success, nil) in
if success {
self.openContacts()
}
else {
print("not authorized")
}
})
}
else if authStatus == CNAuthorizationStatus.authorized {
self.openContacts()
}
}
func openContacts() {
let contactPicker = CNContactPickerViewController.init()
contactPicker.delegate = self as CNContactPickerDelegate
self.present(contactPicker, animated: true, completion: nil)
}
func contactPickerDidCancel(_picker: CNContactPickerViewController) {
_picker.dismiss(animated: true) {
}
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
// when a contact is selected get the full name
let fullName = "\(contact.givenName) \(contact.familyName)"
// the following gets the email address from contact
var email = "No Email Address Found"
if !contact.emailAddresses.isEmpty {
let emailString = (((contact.emailAddresses[0] as AnyObject).value(forKey: "labelValuePair")
as AnyObject).value(forKey: "value"))
email = emailString! as! String
}
// start of adding postaladdresses
var streetAddress = "No Postal Address Found"
if !contact.postalAddresses.isEmpty {
let streetAddressString = (((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair")
as AnyObject).value(forKey: "value"))
streetAddress = streetAddressString! as! String
}
// end of postal addresses
// the following takes the information and populates a label on the view controller
self.lblDetails.text = "\(fullName)\n\(email)\n\(streetAddress)" // \n\(streetName)
}
}
你必须这样使用:
if let addressString = (((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair") as AnyObject).value(forKey: "value")) as? CNPostalAddress {
let mailAddress = CNPostalAddressFormatter.string(from:addressString, style: .mailingAddress)
streetAddress = "\(addressString.street), \(addressString.city), \(addressString.state), \(addressString.postalCode), \(addressString.country)"
streetAddress = "\(mailAddress)
}
你可以这样得到 address
:
streetAddress = "\(addressString.street), \(addressString.city), \(addressString.state), \(addressString.postalCode), \(addressString.country)"
或者你可以这样 mailing address
:
let mailAddress = CNPostalAddressFormatter.string(from:addressString, style: .mailingAddress)
streetAddress = "\(mailAddress)
我正在 Xcode 8 和 swift 3 中编写一个应用程序,我想在其中提取街道、城市、州、国家和邮政编码的邮政地址详细信息,并将它们填充到视图控制器上的标签。我已经修改了允许授权的 info.plist 文件,并且我编写了以下代码 below.I 创建了一个标签来保存信息以及一个用于访问联系人的按钮。
我能够获得填充到标签中的联系人的全名和电子邮件地址,但我不知道如何从邮寄地址。我已在 Stack Overflow 和互联网上进行了穷尽搜索,但无济于事。谨请求您的帮助。
欢迎提出任何见解和建议。
import UIKit
import Contacts
import ContactsUI
class ThirdViewController: UIViewController, CNContactPickerDelegate {
@IBOutlet weak var lblDetails: UILabel!
@IBOutlet weak var backTextField: UITextView!
@IBAction func btnSelectContact(_ sender: Any) {
let entityType = CNEntityType.contacts
let authStatus = CNContactStore.authorizationStatus(for:entityType)
if authStatus == CNAuthorizationStatus.notDetermined {
let contactStore = CNContactStore.init()
contactStore.requestAccess(for: entityType, completionHandler: {(success, nil) in
if success {
self.openContacts()
}
else {
print("not authorized")
}
})
}
else if authStatus == CNAuthorizationStatus.authorized {
self.openContacts()
}
}
func openContacts() {
let contactPicker = CNContactPickerViewController.init()
contactPicker.delegate = self as CNContactPickerDelegate
self.present(contactPicker, animated: true, completion: nil)
}
func contactPickerDidCancel(_picker: CNContactPickerViewController) {
_picker.dismiss(animated: true) {
}
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
// when a contact is selected get the full name
let fullName = "\(contact.givenName) \(contact.familyName)"
// the following gets the email address from contact
var email = "No Email Address Found"
if !contact.emailAddresses.isEmpty {
let emailString = (((contact.emailAddresses[0] as AnyObject).value(forKey: "labelValuePair")
as AnyObject).value(forKey: "value"))
email = emailString! as! String
}
// start of adding postaladdresses
var streetAddress = "No Postal Address Found"
if !contact.postalAddresses.isEmpty {
let streetAddressString = (((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair")
as AnyObject).value(forKey: "value"))
streetAddress = streetAddressString! as! String
}
// end of postal addresses
// the following takes the information and populates a label on the view controller
self.lblDetails.text = "\(fullName)\n\(email)\n\(streetAddress)" // \n\(streetName)
}
}
你必须这样使用:
if let addressString = (((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair") as AnyObject).value(forKey: "value")) as? CNPostalAddress {
let mailAddress = CNPostalAddressFormatter.string(from:addressString, style: .mailingAddress)
streetAddress = "\(addressString.street), \(addressString.city), \(addressString.state), \(addressString.postalCode), \(addressString.country)"
streetAddress = "\(mailAddress)
}
你可以这样得到 address
:
streetAddress = "\(addressString.street), \(addressString.city), \(addressString.state), \(addressString.postalCode), \(addressString.country)"
或者你可以这样 mailing address
:
let mailAddress = CNPostalAddressFormatter.string(from:addressString, style: .mailingAddress)
streetAddress = "\(mailAddress)