Last active
April 20, 2020 01:35
-
-
Save akacaptain/58cc0f8073f436dabc869ed2aa6f3830 to your computer and use it in GitHub Desktop.
PhoneNumberKit and SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import UIKit | |
import PhoneNumberKit | |
struct PhoneNumberTextFieldView: UIViewRepresentable { | |
@Binding var phoneNumber: String | |
private let textField = PhoneNumberTextField() | |
func makeUIView(context: Context) -> PhoneNumberTextField { | |
textField.withExamplePlaceholder = true | |
//textField.font = UIFont(name: GlobalConstant.paragraphFont.rawValue, size: 17) | |
textField.withFlag = true | |
textField.withPrefix = true | |
// textField.placeholder = "Enter phone number" | |
textField.becomeFirstResponder() | |
return textField | |
} | |
func getCurrentText() { | |
self.phoneNumber = textField.text! | |
} | |
func updateUIView(_ view: PhoneNumberTextField, context: Context) { | |
} | |
} | |
struct SignUpSignInView: View { | |
@State private var phoneNumber = String() | |
@State private var validationError = false | |
@State private var errorDesc = Text("") | |
@State private var phoneField: PhoneNumberTextFieldView? | |
let phoneNumberKit = PhoneNumberKit() | |
var body: some View { | |
ZStack { | |
VStack(alignment: .leading) { | |
Text("Sign in with your phone number") | |
self.phoneField | |
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 60) | |
.keyboardType(.phonePad) | |
Button(action: { | |
do { | |
self.phoneField?.getCurrentText() | |
print("phone is: \(self.phoneNumber)") | |
let validatedPhoneNumber = try self.phoneNumberKit.parse(self.phoneNumber) | |
print("Validated Number: \(validatedPhoneNumber)") | |
// Integrate with your login/registration system here... | |
} | |
catch { | |
self.validationError = true | |
self.errorDesc = Text("Please enter a valid phone number") | |
} | |
}) { | |
Text("Sign in") | |
} | |
.padding(15) | |
Spacer() | |
} | |
.padding() | |
.onAppear { | |
self.phoneField = PhoneNumberTextFieldView(phoneNumber: self.$phoneNumber) | |
} | |
.alert(isPresented: self.$validationError) { | |
Alert(title: Text(""), message: self.errorDesc, dismissButton: .default(Text("OK"))) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment