marmelroy/PhoneNumberKit là dự án tập trung di động trên GitHub với 5.4k sao, viết chủ yếu bằng Swift. A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
This repository is frozen at 4.3.0 and no longer receives metadata updates, so its validation
data will drift out of date over time. PhoneNumberKit was created and maintained here for years by
Roy Marmelstein — thank you. Maintenance has moved to the organization to keep the metadata
current and the project sustainable.
Migrating: point your package at https://github.com/PhoneNumberKit/PhoneNumberKit — the module
name PhoneNumberKit is unchanged, so core usage needs no source changes. If you use the text field
or picker, add the PhoneNumberKitUI package and import PhoneNumberKitUI. Full steps in the
migration guide.
PhoneNumberKit
Swift 5.3 framework for parsing, formatting and validating international phone numbers.
Inspired by Google's libphonenumber.
Features
Features
:phone:
Validate, normalize and extract the elements of any phone number string.
:100:
Simple Swift syntax and a lightweight readable codebase.
:checkered_flag:
Fast. 1000 parses -> ~0.4 seconds.
:books:
Best-in-class metadata from Google's libPhoneNumber project.
:trophy:
Fully tested to match the accuracy of Google's JavaScript implementation of libPhoneNumber.
:iphone:
Built for iOS. Automatically grabs the default region code from the phone.
📝
Editable (!) AsYouType formatter for UITextField.
:us:
Convert country codes to country names and vice versa
Usage
Import PhoneNumberKit at the top of the Swift file that will interact with a phone number.
import PhoneNumberKit
All of your interactions with PhoneNumberKit happen through a PhoneNumberUtility object. The first step you should take is to allocate one.
A PhoneNumberUtility instance is relatively expensive to allocate (it parses the metadata and keeps it in memory for the object's lifecycle), you should try and make sure PhoneNumberUtility is allocated once and deallocated when no longer needed.
let phoneNumberUtility = PhoneNumberUtility()
To parse a string, use the parse function. The region code is automatically computed but can be overridden if needed. PhoneNumberKit automatically does a hard type validation to ensure that the object created is valid, this can be quite costly performance-wise and can be turned off if needed.
do {
let phoneNumber = try phoneNumberUtility.parse("+33 6 89 017383")
let phoneNumberCustomDefaultRegion = try phoneNumberUtility.parse("+44 20 7031 3000", withRegion: "GB", ignoreType: true)
}
catch {
print("Generic parser error")
}
If you need to parse and validate a large amount of numbers at once, PhoneNumberKit has a special, lightning fast array parsing function. The default region code is automatically computed but can be overridden if needed. Here you can also ignore hard type validation if it is not necessary. Invalid numbers are ignored in the resulting array.
let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
let phoneNumbers = phoneNumberUtility.parse(rawNumberArray)
let phoneNumbersCustomDefaultRegion = phoneNumberUtility.parse(rawNumberArray, withRegion: "DE", ignoreType: true)
PhoneNumber objects are immutable Swift structs with the following properties:
phoneNumber.numberString
phoneNumber.countryCode
phoneNumber.nationalNumber
phoneNumber.numberExtension
phoneNumber.type // e.g Mobile or Fixed
Formatting a PhoneNumber object into a string is also very easy
To use the AsYouTypeFormatter, just replace your UITextField with a PhoneNumberTextField (if you are using Interface Builder make sure the module field is set to PhoneNumberKit).
You can customize your TextField UI in the following ways
withFlag will display the country code for the currentRegion. The flagButton is displayed in the leftView of the text field with it's size set based off your text size.
withExamplePlaceholder uses attributedPlaceholder to show an example number for the currentRegion. In addition when withPrefix is set, the country code's prefix will automatically be inserted and removed when editing changes.
PhoneNumberTextField automatically formats phone numbers and gives the user full editing capabilities. If you want to customize you can use the PartialFormatter directly. The default region code is automatically computed but can be overridden if needed (see the example given below).
class MyGBTextField: PhoneNumberTextField {
override var defaultRegion: String {
get {
return "GB"
}
set {} // exists for backward compatibility
}
}
Please refer to CountryCodePickerOptions for more information about usage and how it affects the view.
Need more customization?
You can access the metadata powering PhoneNumberKit yourself, this enables you to program any behaviours as they may be implemented in PhoneNumberKit itself. It does mean you are exposed to the less polished interface of the underlying file format. If you program something you find useful please push it upstream!
The Swift Package Manager is now the preferred tool for distributing PhoneNumberKit.
From Xcode 11+ :
Select File > Swift Packages > Add Package Dependency. Enter https://github.com/marmelroy/PhoneNumberKit.git in the "Choose Package Repository" dialog.
In the next page, specify the version resolving rule as "Up to Next Major" from "4.0.0".
After Xcode checked out the source and resolving the version, you can choose the "PhoneNumberKit" library and add it to your app target.
For more info, read Adding Package Dependencies to Your App from Apple.
Alternatively, you can also add PhoneNumberKit to your Package.swift file:
marmelroy/PhoneNumberKit có 5.4k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/marmelroy/PhoneNumberKit. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
marmelroy/PhoneNumberKit có những chủ đề gì?
GitHub topics của marmelroy/PhoneNumberKit: "contacts", "formatting", "google-libphonenumber", "parsing", "phone-number", "swift", "validation". TopGit xếp repo vào nhóm Mobile.
marmelroy/PhoneNumberKit còn đang phát triển không?
Commit gần nhất trên marmelroy/PhoneNumberKit là 2 tháng trước (theo timestamp GitHub). Repo có 854 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
marmelroy/PhoneNumberKit là gì?
marmelroy/PhoneNumberKit (marmelroy/PhoneNumberKit) là dự án Swift trên GitHub. Theo mô tả gốc: A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
marmelroy/PhoneNumberKit so với các dự án Mobile khác thế nào?
marmelroy/PhoneNumberKit được TopGit xếp vào nhóm Mobile, với 5.4k sao GitHub và viết bằng Swift. Xem trang chủ đề Mobile trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
marmelroy/PhoneNumberKit viết bằng ngôn ngữ gì?
marmelroy/PhoneNumberKit chủ yếu viết bằng Swift. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao marmelroy/PhoneNumberKit được xếp vào nhóm Mobile?
TopGit xếp marmelroy/PhoneNumberKit vào nhóm Mobile dựa trên GitHub topics và mô tả của repo (gắn thẻ: "contacts", "formatting", "google-libphonenumber"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về PhoneNumberKit?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về PhoneNumberKit.