从联系 Swift 获取街道地址

Posted

技术标签:

【中文标题】从联系 Swift 获取街道地址【英文标题】:Get Street Address From Contact Swift 【发布时间】:2016-12-08 12:47:05 【问题描述】:

我创建了一个应用,用户可以选择联系人,然后获取姓名、生日和街道地址。 我可以获取并显示姓名和生日。但我无法获得街道地址

这是我的代码

@IBOutlet weak var names: UILabel!
@IBOutlet weak var birthday: UILabel!
@IBOutlet weak var address: UILabel!

var storeNames = ""
var storeBirthday = ""
var storeAddress = CNLabeledValue()

override func viewWillAppear(animated: Bool) 
    super.viewWillAppear(true)
    names.text = storeNames
    birthday.text = storeBirthday
    address.text = String(storeAddress)


func getDateStringFromComponents(dateComponents: NSDateComponents) -> String! 
    if let date = NSCalendar.currentCalendar().dateFromComponents(dateComponents) 
        let dateFormatter = NSDateFormatter()
        dateFormatter.locale = NSLocale.currentLocale()
        dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
        let dateString = dateFormatter.stringFromDate(date)

        return dateString
    
    return nil


@IBAction func showContacts(sender: AnyObject) 
    let contactPickerViewController = CNContactPickerViewController()
    contactPickerViewController.predicateForEnablingContact = NSPredicate(format: "birthday != nil")
    contactPickerViewController.delegate = self
    presentViewController(contactPickerViewController, animated: true, completion: nil)


func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) 
    storeNames = contact.givenName //Get Names
    if let bday = contact.birthday?.date as NSDate! 
        let formatter = NSDateFormatter()
        formatter.timeZone = NSTimeZone(name: "UTC")
        formatter.dateFormat = "dd/MM/yyyy"
        let stringDate = formatter.stringFromDate(contact.birthday!.date!)
        //Their birthday as a String:
        storeBirthday = stringDate //Get Birthday
    

    storeAddress = contact.postalAddresses[0]
    print(storeAddress)

这样打印

<CNLabeledValue: 0x7fca63c12780: identifier=150C06A9-8008-4496-B27A- CE2648622793, label=_$!<Work>!$_, value=<CNPostalAddress: 0x7fca63c0dce0: street=165 Davis Street, city=Hillsborough, state=CA, postalCode=94010, country=, countryCode=us, formattedAddress=(null)>> 2016-12-08 19:35:09.920 Birthdays[638:9739] plugin com.apple.MobileAddressBook.ContactsViewService invalidated

对不起,我不知道如何获取街道地址

【问题讨论】:

storeAddressCNPostalAddress 对象。那么let streetAddress = storeAddress.street? Value of type 'CNLabeledValue' has no member 'street' let streetAddress = storeAddress.value.street? value of type 'protocol&lt;NSCopying, NSSecureCoding&gt;' has no member 'street' 【参考方案1】:

您必须将 storeAddress 的值转换为具有所需属性的 CNPostalAddress 对象

if let address = storeAddress.value as? CNPostalAddress 

   let city = address.city
   ...

【讨论】:

【参考方案2】:

我发现将地址解析为单个字符串的最简单方法是这样的:

let emailAddress = contact.emailAddresses.count > 0 ? contact.emailAddresses[0].value : ""
let address = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value.street)" : ""
let city = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value.city)" : ""
let state = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value.state)" : ""
let zipCode = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value.postalCode)" : ""
let country = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value.country)" : ""

我得到这样的值:

contact.postalAddresses[0].value.street

【讨论】:

以上是关于从联系 Swift 获取街道地址的主要内容,如果未能解决你的问题,请参考以下文章

从 Whatsapp 地址获取联系人姓名

如何从联系人列表中获取电子邮件地址?

从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]

我可以从 iCloud 获取联系人的电子邮件地址吗?

我想在 android 联系人中填充新联系人或现有联系人中的地址

快速从地址簿框架返回空值