PhoneNumberUtil
public protocol PhoneNumberUtil
PhoneNumberUtil provides functionality to parse and format phone numbers, and to test whether a string is a possible phone number.
-
Parse a string as a phone number.
The returned phone number object can then be passed to the formatting method
PhoneNumberUtil.parse
.Throws
PhoneNumberParseError
Declaration
Swift
func parse(_ input: String, defaultRegion: String) throws -> PhoneNumber;
Parameters
input
String to be parsed
defaultRegion
default region as ISO-3166-1 country code.
Return Value
a PhoneNumber
-
Format a phone number in the specified format.
Declaration
Swift
func format(_ phoneNumber: PhoneNumber, format: PhoneNumberFormat) -> String;
Parameters
phoneNumber
a phone number instance.
format
a format specifier, e.g. E.164.
Return Value
The formatted phone number. Returns empty string if input is invalid.
-
Get an example phone number.
Defaults to returning a Mobile example number.
Declaration
Swift
func exampleNumber(_ isoCountryCode: String) -> PhoneNumber?;
Parameters
isoCountryCode
region / country as ISO-3166-1 country code.
Return Value
an example phone number. May return nil if the input region country code is invalid.
-
Checks whether input is a possible phone number, given a ISO-3166-1 country code where the number would be dialed from.
Note that even if this method indicates that the input is a possible phone number, it doesn’t necessarily mean it’s a valid phone number. I.e. this is a less strict check.
Declaration
Swift
func isPossibleNumber(_ string: String, fromRegion: String) -> (possible: Bool, error: PhoneNumberValidationError?);
Parameters
string
string to test whether it’s a viable phone number.
fromRegion
region / country as ISO-3166-1 country code where the number will be dialed from.
Return Value
A Bool
possible
that indicated whether the input is a viable phone number. An optional error indicating why the input is not considered a viable phone number. -
Checks whether a parsed phone number is a viable phone number.
Declaration
Swift
func isPossibleNumber(_ phoneNumber: PhoneNumber) -> (possible: Bool, error: PhoneNumberValidationError?);
Parameters
phoneNumber
a parsed phone number to test whether it’s a possible / viable phone number.
Return Value
A Bool
possible
that indicated whether the input is a viable phone number. An optional error indicating why the input is not considered a viable phone number. -
Returns a region / country list structure that contains info on all countries / regions available by NSLocale.ISOCountryCodes. Each entry in the list provides info such as ISO 3166-1 country code, country dialing code, and country display name (according to the given locale).
Declaration
Swift
func regionList(forLocale locale:Locale) -> SINRegionList;
Parameters
forLocale
locale that will determine display names for regions.
Return Value
a region list (note, it’s a bridged Objective-C type).