How to determine region from the phone number string? - ios

I tried using PhoneNumberKit and couldn't find any proper api which would give me the region name.
I need the region from the phone number so that I can display the appropriate flag. For example using this:
let phoneNumber = try phoneNumberKit.parse("+12563335956")
let regionCode = phoneNumberKit.countries(withCode: phoneNumber.countryCode)?.first
print("region code is: " , regionCode)
// US phone number with +1 prefix, but it prints "AG" which is wrong.

As you said, the country code can tell you which flag to use.
let phoneNumber = try phoneNumberKit.parse("+1 970162651778")
let regionCode = phoneNumberKit.getRegionCode(of: phoneNumber)
print(regionCode) // Optional("US")
You can get the country region code using the country code of the phone number.

Related

How do I differentiate between two countries with the same country code using libPhoneNumber-iOS?

I am using libPhoneNumber-iOS (https://github.com/iziz/libPhoneNumber-iOS) to parse international phone numbers, and I am having a bit of trouble differentiating between different countries with the same country code (e.g. USA and Canada both use +1). Does anyone know if this library does this?
Here is the code I'm using to retrieve the country code (region code i.e. "US"):
let fullPhoneNumber = "+15065552222"
guard let phoneUtil = NBPhoneNumberUtil.sharedInstance() else {
return "error"
}
do {
let phoneNumber = try phoneUtil.parse(fullPhoneNumber, defaultRegion: "ZZ")
return phoneUtil.getRegionCode(forCountryCode: phoneNumber.countryCode)
} catch {
return "error"
}
This code returns "US" for the country code, even though it is a Canadian number. Does anyone know what I can do to get the accurate country using this library?

Getting full address string from CLPLacemark using Swift 4 / Xcode 9

I have previously been able to get a full address String from a CLPlacemark using the following code for Swift 3:
let addressList = placemark.addressDictionary?["FormattedAddressLines"] as? [String]
let address = addressList!.joined(separator: "\n")
addressDictionary is now deprecated in swift 4.
I could extract each of the individual CLPlacemark address variable strings (name, country postalCode, etc) but I'm wondering if there is a simpler way to do it.
I know there is a postalAddress var which is of type CNPostalAddress but not sure how to convert that to a String.
You can get mailing address from CNPostalAddress is like :
CNPostalAddressFormatter.string(from:YOUR_POSTAL_ADDRESS, style: .mailingAddress)

Can I use iOS9 Contacts Framework to get a formatted phone number?

I would like to be able to store a phone number in a standard way, e.g. just the digits (potentially with the '+' for the country code), something like these examples...
"17185555555"
"+17185555555"
"+447788888888"
... but I'd like to be able to DISPLAY it to a user in a properly formatted way, e.g.
"1 (718) 555-5555"
"+1 (718) 555-5555"
"+44 (7788) 888888"
...WITHOUT having to rely on a CNContactViewController to format it.
Obviously doing this just for US/Canada numbers would be easy - it's just one standard format - but I'd like it to be generic so it can work for numbers from any country. I know this question gives an answer for US/Can numbers only.
I know that I could use a CNContactViewController to display the number in the correct format, e.g.
let newContact = CNMutableContact()
newContact.givenName = "John"
newContact.familyName = "Smith"
newContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberiPhone, value: CNPhoneNumber(stringValue:"17185555555"))]
let contactView = CNContactViewController(forContact: newContact)
self.presentViewController(contactView, animated: true, completion: nil)
This will show the number on screen properly formatted, i.e.
1 (718) 555-5555
... so I know that something in the framework can do it. (This approach works for other country phone number formats, as long as you prefix the number with the right country code - e.g. "+44...")
From various other questions I know that I can get the raw digits and country code out of a CNContact, e.g. (following above example)
for pNumber: CNLabeledValue in newContact.phoneNumbers {
let value = pNumber.value as! CNPhoneNumber
let cc = value.valueForKey("countryCode") as? String
let digits = value.valueForKey("digits") as? String
print("cc:" + cc + ", " + digits)
}
... but this will just show the unformatted string again - not what I am looking for in this case.
Any help or other recommended approaches really appreciated!
My answer proposes another lib
You can format your numbers with this lib.
And you can use like this:
let phoneNumber: NBPhoneNumber = try phoneUtil.parse("17185555555", defaultRegion: yourRegion)
let formattedString: String = try phoneUtil.format(phoneNumber, numberFormat: .E164)

Access Users Contacts Phone Numbers CNContacts

I am trying to get to phone numbers of my contacts using CNContact, i want to have the number as a simple string of its didgits such as "04xxxxxxxx" but the closest I can get to is the following. ("contact" is of type CNContact)
contact.phoneNumbers[0].value
\\Which prints: <CNPhoneNumber: 0x13560a550: countryCode=au, digits=04xxxxxxxx>
ive tried all the obvious things and not so obvious things, thanks
If anyone has a more legitimate solution please do post it, otherwise this rather hacky approach works:
let value = String(contact.phoneNumbers[0].value)
let start = value.rangeOfString("digits=")!.endIndex
let end = value.endIndex.predecessor()
let number = value.substringWithRange(Range<String.Index>(start: start, end: end))
Using this category you can now access the phone number via swift.
don't forget to include this file in the bridging header
#implementation CNPhoneNumber (SwiftSupport)
- (NSString*)toString {
return self.stringValue;
}
#end
Fetch the number value as follows:
let number = value.valueForKey("digits") as! String
From iOS9:
let number = value.stringValue
According to Apple's documentation on CNPhoneNumber:
stringValue
Property
The string value of the phone number. (read-only)
Declaration
SWIFT
var stringValue: String { get }

How to get country specific result with Google autocomplete place api ios sdk?

I'm using below method to get autocomplete result for input string of GMSPlacesClient class:
- (void)autocompleteQuery:(NSString *)query
bounds:(GMSCoordinateBounds * GMS_NULLABLE_PTR)bounds
filter:(GMSAutocompleteFilter * GMS_NULLABLE_PTR)filter
callback:(GMSAutocompletePredictionsCallback)callback
I tried to implement bounds by multiple ways but none of them worked, I wonder if there is any standard way to get country specific results with GMSCoordinateBounds?
You can get the country specific results with the Google autocomplete place api iOS SDK. Just go to this link
let filter = GMSAutocompleteFilter()
filter.type = .Establishment
filter.country = "UK"
set the filter country to what ever the country code you want. This will then fetch only country specific results for you.
You can obtain the country codes from this link
Hope this helps.
In your google places API url, just append
&components=country:sg
here sg = country code for singapore. Append country code as per your requirement.
The country must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. you can refer Country code Of different countries
This one work for me
let placesClient = GMSPlacesClient()
let filter = GMSAutocompleteFilter()
filter.type = .Establishment
filter.country = "SG"
placesClient.autocompleteQuery(searchString, bounds: nil, filter: filter) { (results, error:NSError?) -> Void in
self.resultsArray.removeAll()
if let _ = results {
for result in results!{
if let res: GMSAutocompletePrediction = result {
// your code
}
}
}
}
in Swift 5 and later
you can custom your search by configuring the GMSAutocompleteFilter, and specify the country from its attribute country
example :
let filter = GMSAutocompleteFilter()
filter.country = "MA"
autocompleteController.autocompleteFilter = filter
the country should be a ISO 3166-1 Alpha-2 country code (case insensitive).
you can find those codes from here: Countries ISO Codes
Looks like Google autocomplete Places API for iOS doesn't support filtering by country. GMSCoordinateBounds uses the northEast and southWest bounds corresponding to the rectangular region defined by the two corners.
It might be easier to use Google Places API Web Service and filter with components=country: You can specify what country you want after country:
The full list of country codes can be found here:
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
Use it with:
let filter = GMSAutocompleteFilter()
filter.country = "YOURCountryCode"

Resources