Get street address from Swift contact

I create an application, the user can select a contact, and then get the name, birthday and street address. I can get and display the name and birthday. But I can’t get the street address

here is my code

@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)
}

he prints like this

<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

Sorry, but I do not know how to get the street address

+4
source share
2 answers

You must specify the value of your storeAddressobject CNPostalAddresswith the properties you need.

if let address = storeAddress.value as? CNPostalAddress {

   let city = address.city
   ...
}
+4
source

The easiest way to find an address processed in separate lines was as follows:

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)" : ""

I get the following values:

contact.postalAddresses[0].value.street
+3
source

Source: https://habr.com/ru/post/1663232/


All Articles