How to get formatted address NSString from AddressDictionary?

前端 未结 9 1400
旧巷少年郎
旧巷少年郎 2020-12-17 14:58

Trying to get formatted address from AddressDictionary, that I got from CLGeocoder. Used following code with no result:

subtitle = [NSString stringWithString         


        
相关标签:
9条回答
  • 2020-12-17 15:44

    The documentation for the addressDictionary property says:

    You can format the contents of this dictionary to get a full address string as opposed to building the address yourself. To format the dictionary, use the ABCreateStringWithAddressDictionary function as described in Address Book UI Functions Reference.

    So add and import the AddressBookUI framework and try:

    subtitle = 
        ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO);
    
    0 讨论(0)
  • 2020-12-17 15:44

    As highlighted by Martyn Davis, ABCreateStringWithAddressDictionary is deprecated in iOS 9.

    You can use the functions below to convert the addressDictionary to the newer CNMutablePostalAddress, then use the CNPostalAddressFormatter to generate a localised string as long as you import the Contacts framework.

    Swift 3.x

    // Convert to the newer CNPostalAddress
    func postalAddressFromAddressDictionary(_ addressdictionary: Dictionary<NSObject,AnyObject>) -> CNMutablePostalAddress {
       let address = CNMutablePostalAddress()
    
       address.street = addressdictionary["Street" as NSObject] as? String ?? ""
       address.state = addressdictionary["State" as NSObject] as? String ?? ""
       address.city = addressdictionary["City" as NSObject] as? String ?? ""
       address.country = addressdictionary["Country" as NSObject] as? String ?? ""
       address.postalCode = addressdictionary["ZIP" as NSObject] as? String ?? ""
    
       return address
    }
    
    // Create a localized address string from an Address Dictionary
    func localizedStringForAddressDictionary(addressDictionary: Dictionary<NSObject,AnyObject>) -> String {
        return CNPostalAddressFormatter.string(from: postalAddressFromAddressDictionary(addressDictionary), style: .mailingAddress)
    }
    

    Swift 2.x

    import Contacts
    
    // Convert to the newer CNPostalAddress
    func postalAddressFromAddressDictionary(addressdictionary: Dictionary<NSObject,AnyObject>) -> CNMutablePostalAddress {
    
        let address = CNMutablePostalAddress()
    
        address.street = addressdictionary["Street"] as? String ?? ""
        address.state = addressdictionary["State"] as? String ?? ""
        address.city = addressdictionary["City"] as? String ?? ""
        address.country = addressdictionary["Country"] as? String ?? ""
        address.postalCode = addressdictionary["ZIP"] as? String ?? ""
    
        return address
    }
    
    // Create a localized address string from an Address Dictionary
    func localizedStringForAddressDictionary(addressDictionary: Dictionary<NSObject,AnyObject>) -> String {
    
        return CNPostalAddressFormatter.stringFromPostalAddress(postalAddressFromAddressDictionary(addressDictionary), style: .MailingAddress)
    }
    
    0 讨论(0)
  • 2020-12-17 15:49

    Swift 5 version

    CLGeocoder().reverseGeocodeLocation(newLocation!, preferredLocale: nil) { (clPlacemark: [CLPlacemark]?, error: Error?) in
                guard let place = clPlacemark?.first else {
                    print("No placemark from Apple: \(String(describing: error))")
                    return
                }
    
                if let addrList = place.addressDictionary?["FormattedAddressLines"] as? [String] {
                    let addressString = addrList.joined(separator: ", ")
                    print(addressString)
    
                }
            }
    
    0 讨论(0)
提交回复
热议问题