KSTPhoneNumberFormatter

@interface KSTPhoneNumberFormatter : NSFormatter

KSTPhoneNumberFormatter is an NSFormatter subclass that can perform locale aware phone number formatting. It also supports formatting as the user types validation by implementing isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription: though the errorDescription parameter is ignored.

  • Returns the shared formatter object.

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) KSTPhoneNumberFormatter *_Nonnull sharedFormatter;
  • The locale to reference when formatting. If set to a non-nil value, the formatter will attempt to format using the specified locale regardless of the user or system locales.

    The default is NSLocale.currentLocale.

    Declaration

    Objective-C

    @property (nonatomic, copy, null_resettable) NSLocale *locale;
  • Returns a formatted string suitable for display from the provided phoneNumber. For example @“1234567890” -> @“(123) 456-7890” in the en locale.

    This method always respects the receiver’s locale.

    Declaration

    Objective-C

    - (nullable NSString *)stringFromPhoneNumber:(nonnull NSString *)phoneNumber;

    Parameters

    phoneNumber

    The phone number to format

    Return Value

    The formatted string

  • Returns a formatted string suitable for display from the provided phoneNumber. For example @“1234567890” -> @“(123) 456-7890” in the en locale.

    This method always uses NSLocale.currentLocale when formatting.

    Declaration

    Objective-C

    - (nullable NSString *)localizedStringFromPhoneNumber:
        (nonnull NSString *)phoneNumber;

    Parameters

    phoneNumber

    The phone number to format

    Return Value

    The formatted string

  • Returns a phone number stripped of all formatting. For example, @“(123) 456-7890” -> @“1234567890”.

    Declaration

    Objective-C

    - (nullable NSString *)phoneNumberFromString:(nonnull NSString *)string;

    Parameters

    string

    The formatted string

    Return Value

    The phone number

  • Returns the numeric phone number suitable for passing to frameworks like CallKit.

    Declaration

    Objective-C

    - (int64_t)numericPhoneNumberFromString:(nonnull NSString *)string;

    Parameters

    string

    The formatted string

    Return Value

    The numeric phone number

  • Strips all formatting from the string except for digit characters and returns @“+” prepended to the resulting string.

    Declaration

    Objective-C

    - (nullable NSString *)E164PhoneNumberFromString:(nonnull NSString *)string;

    Parameters

    string

    The string to format

    Return Value

    The E.164 formatted phone number or nil