Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Created March 21, 2018 14:15
Show Gist options
  • Save iAmrSalman/09143e373a748ded9109c831474bed7d to your computer and use it in GitHub Desktop.
Save iAmrSalman/09143e373a748ded9109c831474bed7d to your computer and use it in GitHub Desktop.
[PhoneNumberManager] validate and format phoneNumber using PhoneNumberKit
import Foundation
import PhoneNumberKit
enum Format {
case e164
case national
case international
}
class PhoneNumberManager {
class func validate(_ number: String, with format: Format) throws -> String {
let phoneKit = PhoneNumberKit()
let phoneNumber = try phoneKit.parse(number, ignoreType: true)
switch format {
case .e164:
return phoneKit.format(phoneNumber, toType: .e164)
case .international:
return phoneKit.format(phoneNumber, toType: .international)
case .national:
return phoneKit.format(phoneNumber, toType: .national)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment