Skip to content

Commit

Permalink
Ignoring international numbers on PhoneNumberTextField (#654)
Browse files Browse the repository at this point in the history
Fix #653
  • Loading branch information
bguidolim authored Jun 12, 2023
1 parent da2e787 commit 0688c44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
29 changes: 25 additions & 4 deletions PhoneNumberKit/PartialFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,28 @@ public final class PartialFormatter {
weak var parser: PhoneNumberParser?
weak var regexManager: RegexManager?

public convenience init(phoneNumberKit: PhoneNumberKit = PhoneNumberKit(), defaultRegion: String = PhoneNumberKit.defaultRegionCode(), withPrefix: Bool = true, maxDigits: Int? = nil) {
self.init(phoneNumberKit: phoneNumberKit, regexManager: phoneNumberKit.regexManager, metadataManager: phoneNumberKit.metadataManager, parser: phoneNumberKit.parseManager.parser, defaultRegion: defaultRegion, withPrefix: withPrefix, maxDigits: maxDigits)
public convenience init(phoneNumberKit: PhoneNumberKit = PhoneNumberKit(),
defaultRegion: String = PhoneNumberKit.defaultRegionCode(),
withPrefix: Bool = true,
maxDigits: Int? = nil,
ignoreIntlNumbers: Bool = false) {
self.init(phoneNumberKit: phoneNumberKit,
regexManager: phoneNumberKit.regexManager,
metadataManager: phoneNumberKit.metadataManager,
parser: phoneNumberKit.parseManager.parser,
defaultRegion: defaultRegion,
withPrefix: withPrefix,
maxDigits: maxDigits,
ignoreIntlNumbers: ignoreIntlNumbers)
}

init(phoneNumberKit: PhoneNumberKit, regexManager: RegexManager, metadataManager: MetadataManager, parser: PhoneNumberParser, defaultRegion: String, withPrefix: Bool = true, maxDigits: Int? = nil) {
init(phoneNumberKit: PhoneNumberKit,
regexManager: RegexManager,
metadataManager: MetadataManager,
parser: PhoneNumberParser, defaultRegion: String,
withPrefix: Bool = true,
maxDigits: Int? = nil,
ignoreIntlNumbers: Bool = false) {
self.phoneNumberKit = phoneNumberKit
self.regexManager = regexManager
self.metadataManager = metadataManager
Expand All @@ -30,6 +47,7 @@ public final class PartialFormatter {
self.updateMetadataForDefaultRegion()
self.withPrefix = withPrefix
self.maxDigits = maxDigits
self.ignoreIntlNumbers = ignoreIntlNumbers
}

public var defaultRegion: String {
Expand All @@ -55,11 +73,14 @@ public final class PartialFormatter {
var prefixBeforeNationalNumber = String()
var shouldAddSpaceAfterNationalPrefix = false
var withPrefix = true
var ignoreIntlNumbers = false

// MARK: Status

public var currentRegion: String {
if self.phoneNumberKit.countryCode(for: self.defaultRegion) != 1 {
if ignoreIntlNumbers, currentMetadata?.codeID == "001" {
return defaultRegion
} else if self.phoneNumberKit.countryCode(for: self.defaultRegion) != 1 {
return currentMetadata?.codeID ?? "US"
} else {
return self.currentMetadata?.countryCode == 1
Expand Down
3 changes: 2 additions & 1 deletion PhoneNumberKit/UI/PhoneNumberTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ open class PhoneNumberTextField: UITextField, UITextFieldDelegate {
public private(set) lazy var partialFormatter: PartialFormatter = PartialFormatter(
phoneNumberKit: phoneNumberKit,
defaultRegion: defaultRegion,
withPrefix: withPrefix
withPrefix: withPrefix,
ignoreIntlNumbers: true
)

let nonNumericSet: CharacterSet = {
Expand Down

0 comments on commit 0688c44

Please sign in to comment.