4

In an app, I am displaying the details of the contacts using ABRecordRef. I am using the keys kABPersonAddressCityKey, kABPersonAddressStateKey, kABPersonAddressZIPKey, kABPersonAddressCountryKey. Everything works fine. But I don't know in which format to display the addresses. What I mean is, if you see the Contacts app, the addresses are displayed in a particular format for different countries. Some examples,

US

Street
City State ZIP
Country

India

Street
Province
City PIN
Country

Australia

Street
Suburb State ZIP
Country

Now I don't know how to find the format for different countries.

1.Is there any way to find the address format based on country/country codes?
2.Is there a way we can get the fully formatted address using a single function, like we use ABRecordCopyCompositeName() to get the full name?

Kara
  • 6,115
  • 16
  • 50
  • 57
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • 1
    possible duplicate of http://stackoverflow.com/questions/7848291/how-to-get-formatted-address-nsstring-from-addressdictionary – Pochi Jun 25 '12 at 05:46
  • Geez! Why you guys didn't tell me this before I offered a bounty? – EmptyStack Jun 25 '12 at 05:55
  • 1
    because bounties are juicy O_O, and I haven't had seen your question. You know you could have typed exactly your question title and come across this? haha – Pochi Jun 25 '12 at 06:07
  • Luis Oscar, ;-) I completely agree with you. Thanks man! – EmptyStack Jun 25 '12 at 06:19

2 Answers2

9

From iOS 9.0 / macOS 10.11 / watchOS 2.0 on you should use CNPostalAddressFormatter instead:

The CNPostalAddressFormatter class formats the postal address in a contact. This class handles international formatting of postal addresses.

Below code is in Swift 3 but it is trivial to convert it to Objc

let postalAddress = CNMutablePostalAddress()
postalAddress.street = street
postalAddress.postalCode = zipCode
postalAddress.city = city
postalAddress.state = state
postalAddress.country = country
postalAddress.isoCountryCode = countryCode

let formattedAddress = CNPostalAddressFormatter.string(from: postalAddress, style: .mailingAddress)

Make sure you set the ISO country code property, this is used to determine the format of the address.

Example:

postalAddress.street = "Main Street 1"
postalAddress.postalCode = "67067"
postalAddress.city = "Ludwigshafen"
postalAddress.state = "Rhineland-Palatinate"
postalAddress.country = "Germany"
postalAddress.isoCountryCode = "DE"

leads to this

Main Street 1

67067 Ludwigshafen

Germany

whereas

postalAddress.isoCountryCode = "US"

leads to

Main Street 1

Ludwigshafen Rhineland-Palatinate 67067

Germany

Community
  • 1
  • 1
HAS
  • 19,140
  • 6
  • 31
  • 53
5

Try this link ABCreateStringWithAddressDictionary

It looks like you need to use: ABCreateStringWithAddressDictionary that will return:

Returns a formatted address from an address property. (From Apple)

Good luck

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • You are most welcomed, I just finished an addressbook app and I have to say that the AdreesBook docs are really crappy. – shannoga Jun 25 '12 at 06:21