Developer

Phone Number Formatter

Phone number formatting is handled as part of address formatting, as the metadata for both types of formatting is in one common XML descriptor file.

To format phone numbers, initialize the MAFAddressFormatter, and set the country code:
// set default locale and country code
[MAFAddressFormatter setDefaultLocale:@"hu"];
NSString* countryCode = @"hu";

Set a default locale as well, because if you do not set a value while formatting, you may get incorrect values.

You can use the phone number formatting capabilities of the MAFAddressFormatter in two ways:

In the first API call (formattedPhoneNumberWithCountryCode), obtain a full formatted string (NSString) that contains the properly formatted phone number with the proper extension suffix.

In the other API call (formattedLocalAndInternationalPhoneNumbersWithCountryCode), create and obtain a MAFPhoneNumber object. From that, you can obtain further types, such as local and international number formats of the input phone number value.

As the formatter is implemented as a singleton, you can get access to any MAFAddressFormatter functionality by calling its class level instance method. This first example creates a complete phone number string representation:
// A) specific phone number based formattings

// EITHER get a full number in text
NSString* formattedPhoneNumberText = [[MAFAddressFormatter instance] formattedPhoneNumberWithCountryCode:countryCode number:@"+36301234567" extension:@"321"];

NSLog(@"FORMATTED VALUE> %@", formattedPhoneNumberText);
This code passes the numbers as an NString, in an international number format that is appropriate for the specified locale. It is important that you set the local or international number form (without any spaces or extra delimiters) specific to the locale or country code, because incorrect numbers are not formatted. You can provide the international number by using the + sign, or by using prefixing zeros (outbound number prefix).

The above code shows: FORMATTED VALUE> +36 30 123-4567 Ext. 321

The second example obtains different international/local/outbound forms for a specific number. It uses the other formatting API call, which returns the MAFPhoneNumber object, by specifying the same country code, number format and extension as the first example. You can obtain the formatted values via the properties of the created phone number instance:
// OR get a phone number object, that holds the various parts, and can be presented in more ways
MAFPhoneNumber* formattedPhoneNumber = [[MAFAddressFormatter instance] formattedLocalAndInternationalPhoneNumbersWithCountryCode:countryCode number:@"+36301234567" extension:@"999"];
NSString* international = formattedPhoneNumber.internationalPhoneNumber;
NSString* internationalWithOutbound = formattedPhoneNumber.internationalPhoneNumberWithCountryOutboundCode;
NSString* local = formattedPhoneNumber.localPhoneNumber;

NSLog(@"FORMATTED VALUE> %@", international);
NSLog(@"FORMATTED VALUE> %@", internationalWithOutbound);
NSLog(@"FORMATTED VALUE> %@", local);
This code presents:

FORMATTED VALUE> +36 30 123-4567 Ext. 999

FORMATTED VALUE> 00 36 30 123-4567 Ext. 999

FORMATTED VALUE> 06 30 123-4567 Ext. 999

The third example shows how to obtain country-code-specific phone number information. MAF must parse the address and phone number metadata file to provide access to this runtime information. To get access to outbound codes, local trunk prefix calling numbers, and the international dial codes, you can use these API calls:
// B) country code based information

NSString* outboundCode = [[MAFAddressFormatter instance] outboundCodeWithCountryCode:countryCode];
NSString* localTrunkPrefix = [[MAFAddressFormatter instance] localTrunkPrefixWithCountryCode:countryCode];
NSString* internationalDialCode = [[MAFAddressFormatter instance] internationalDialCodeWithCountryCode:countryCode];

NSLog(@"FORMATTED VALUE> %@", outboundCode);
NSLog(@"FORMATTED VALUE> %@", localTrunkPrefix);
NSLog(@"FORMATTED VALUE> %@", internationalDialCode);
This code presents:

FORMATTED VALUE> 00

FORMATTED VALUE> 06

FORMATTED VALUE> +36

The full example, including the three examples mentioned above:
// set default locale and country code
[MAFAddressFormatter setDefaultLocale:@"hu"];
NSString* countryCode = @"hu";


// A) specific phone number based formattings

// EITHER get a full number in text
NSString* formattedPhoneNumberText = [[MAFAddressFormatter instance] formattedPhoneNumberWithCountryCode:countryCode number:@"+36301234567" extension:@"321"];

NSLog(@"FORMATTED VALUE> %@", formattedPhoneNumberText);


// OR get a phone number object, that holds the various parts, and can be presented in more ways
MAFPhoneNumber* formattedPhoneNumber = [[MAFAddressFormatter instance] formattedLocalAndInternationalPhoneNumbersWithCountryCode:countryCode number:@"+36301234567" extension:@"999"];
NSString* international = formattedPhoneNumber.internationalPhoneNumber;
NSString* internationalWithOutbound = formattedPhoneNumber.internationalPhoneNumberWithCountryOutboundCode;
NSString* local = formattedPhoneNumber.localPhoneNumber;

NSLog(@"FORMATTED VALUE> %@", international);
NSLog(@"FORMATTED VALUE> %@", internationalWithOutbound);
NSLog(@"FORMATTED VALUE> %@", local);


// B) country code based information

NSString* outboundCode = [[MAFAddressFormatter instance] outboundCodeWithCountryCode:countryCode];
NSString* localTrunkPrefix = [[MAFAddressFormatter instance] localTrunkPrefixWithCountryCode:countryCode];
NSString* internationalDialCode = [[MAFAddressFormatter instance] internationalDialCodeWithCountryCode:countryCode];

NSLog(@"FORMATTED VALUE> %@", outboundCode);
NSLog(@"FORMATTED VALUE> %@", localTrunkPrefix);
NSLog(@"FORMATTED VALUE> %@", internationalDialCode);