Contact address format for different countries - ios

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?

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

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

Related

How to filter GMSPlacesAutocompleteTypeFilter to obtain the Country result for both United states and canada

let filter = GMSAutocompleteFilter()
filter.type = GMSPlacesAutocompleteTypeFilter.city
filter.country = "USA" || "CA"
how to get 2 country results in google autocomplete
If you check country property in GMSAutocompleteFilter class then you will found that it is string so I think you can't set multiple country in your filter. If it's allowing multiple country then it should be array or any collection instead of string! and in documentation also they haven't mentioned for multiple countries in filter! So, I think you can't set more than one country at a time in one filter!
It's currently not possible. I would recommend using the web API, where you can send up to 5 ISO country codes in your query (see components under "optional parameters":
https://developers.google.com/places/web-service/autocomplete
It's possible to do using following syntax
filter.country = "CA|country:US"
reference link here.

Google Places Autocomplete API filter type "address" returns places of type "route" via iOS SDK

Google Places API for iOS version: 2.2.30010.0
Code:
let filter = GMSAutocompleteFilter()
filter.type = .address
filter.country = "us"
return filter
When searching for example Montrose with filter of type address & country us, the search results display:
The country filter works, but the type filter displays results of type route. Is this the intended behavior?
The Place Autocomplete docs specify:
address instructs the Place Autocomplete service to return only geocoding results with a precise address. Generally, you use this request when you know the user will be looking for a fully specified address.
Maybe I'm misunderstanding what a precise address is, but it seems like the query should only return results with a building number ex. 22 Montrose Ave.
Is it possible to return only places that have building numbers?
You just need to remove filter.type = .address

How would you match two similar phone numbers in Swift, but one contains the country code and the other one does or does not

I am trying to match two similar phone numbers in swift, but one of them is preceded by a country code and the other one can vary.
For instance, I want these 2 phone numbers to match:
0499999999
+32499999999
I want this to be valid for any phone number all over the world. Is there a regex for this or maybe a pod I could download for doing this?
Thanks
Use PhoneNumberKit To Parse PhoneNumber.
https://github.com/marmelroy/PhoneNumberKit
Like :
let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
let phoneNumbers = phoneNumberKit.parse(rawNumberArray)
let phoneNumbersCustomDefaultRegion = phoneNumberKit.parse(rawNumberArray, withRegion: "DE", ignoreType: true)
You could make a lookup table of countries and country codes and check from the phone what their locale is
let countrycode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String
let countryPrefixes = ["IE": "+353",
"US": "+1",
etc...
let prefix = countryPrefixes[countrycode]
Once you have the prefix, you should be able to look at just the remaining numbers.
Or you could use this github library https://github.com/rmaddy/RMPhoneFormat which seems to have that work done for you already.
Of course this is assuming that you are expecting a phone number entered into your device that is valid in the Locale that the phone is operating in.

Definite article before country name in sentence

I have an iOS app where I want to include the user's country (as defined by region) in a text string, e.g. “In Portugal this is …”. For most country names this is easy:
let countryCode = locale.objectForKey(NSLocaleCountryCode) as! String
let countryName = locale.displayNameForKey(NSLocaleCountryCode,
value: countryCode)!
let text = "In \(countryName) this is …"
However, for some country names, the name should be prefixed by “the”, for example “the Netherlands” and “the United States”. Is this information available in NSLocale somehow, or are there other libraries that can help with this?

CoreData : suggestions for model design

I'm new with Core Data, and I don't know how to proceed to do what I want.
Actually, I would like to make a model with countries and country codes.
BUT: the countries are localized (3 languages for now).
So, here is my model at this time:
Entity: Country
Attributes:
- name_en (String)
- name_nl (String)
- name_fr (string)
Now, I would like associate country codes for each country.
Example:
- Belgium => BE
- United States => USA
- France => FR
etc. but it has to work in French and Dutch too.
I don't know how to link this country codes to name_en, name_nl and name_fr.
Important: The countries are at the same index in French, English or Dutch:
Belgium (en) => index 3
België (nl) => index 3
Belgique (fr) => index 3
Hope I've been clear ;)
Thank you guys.
I would advise to use the NSLocale APIs to cover most of this logic. That would greatly simplify your data structure and instead put the complexity where it belongs - and where Apple can do the heavy lifting for you.
The model would be as simple as this: Entity Country with a string attribute countryCode, which is the string used by Apple to identify each country. These are strings like
en_US
fr_BE
nl_BE
nl_NL
fr_FR
If you do not want duplicate entries for the same country with different language region, you could split them into Country and Language and construct the identifier yourself.
You can generate all the rest with Apple's APIs. E.g., the country code
Country *belgium; // assume localeIdentifier "fr_BE" for French
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:belgium.localeIdentifier];
NSString *belgiumCountryCode = [locale objectForKey:NSLocaleCountryCode];
// --> "BE"
NSString *nameOfBelgiumInFrench =
[locale displayNameForKey:NSLocaleCountryCode value:belgiumCountryCode];
// --> "Belgique"
NSLocale *usLocale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
NSString nameOfBelgiumInEnglish =
[usLocale displayNameForKey:NSLocaleCountryCode value:belgiumCountryCode];
// --> "Belgium"
You can put those cumbersome methods into convenience accessors of your managed object subclasses. Theoretically you could also hard-code them, but I would strongly recommend against it.
One last thing: if you are planning on using Core Data, forget about indexes.

Resources