Swift - How to limit the a few specific text field only allow to insert int?
Try this one.
class ViewController: UIViewController,UITextFieldDelegate {
@IBOutlet var yourTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
yourTextField.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//For mobile numer validation
if textField == yourTextField {
//Add specific int numbers
let allowedCharacters = CharacterSet(charactersIn:"0123 ")//Here change this characters based on your requirement
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
return true
}
}
Native UITextField Secure Text Entry forces English (US) keyboard
This was truly an iOS bug => corrected in iOS 12.1
How to auto fill OTP textfield on OTP code received from firebase in iOS, Swift? (WITHOUT TAPPING)
By design, iOS apps have no access to incoming SMS messages. There are many discussions out there on a similar topic.
- Link 1
- Link 2
- Link 3
- Link 4
The main reason for the restriction is privacy. There is no documented public API to receive a callback when SMS is received. If you try to hack iOS and listen for private notifications then might be your app will be rejected by the App Review team.
While doing research on this topic I found something interesting, you can create an extension that can receive incoming SMS and call events for the purpose of SPAM filtering. This type of extension is called the SMS and Call reporting extension in Apple's ecosystem. While looking at the documentation of this extension, at first glance I thought we can achieve the asked behavior if we can notify our host app about the incoming messages. But while scrolling to the end of the document I found that by design this extension has some additional restrictions:
- The extension cannot share data with the host app.
- The extension can't access the network directly.
- The extension receives the event only if the sender's number is not on the contact list
Anyways it's all about docs, I have not tried anything on the extension. What you can do is try to create an extension and see for other possible solutions.
Example:
1 bad solution in my mind is your extension call your server with the SMS content and then your server forward the SMS content in a silent push notification payload. When your app received the silent notification it serves the purpose of prefilling the field. But again this is not the intended purpose of the extension.
In my opinion, the best is what you have already achieved i.e. setting the text field's content type.
Password autofill is not working in swift
Try naming your text fields serially txt1 txt2 txt3 txt4 tt5 txt6
override func viewDidLoad() {
super.viewDidLoad()
txt1.becomeFirstResponder()
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if ((textField.text?.count)! < 1 ) && (string.count > 0) {
if textField == txt1 {
txt2.becomeFirstResponder()
}
if textField == txt2 {
txt3.becomeFirstResponder()
}
if textField == txt3 {
txt4.becomeFirstResponder()
}
if textField == txt4 {
txt5.becomeFirstResponder()
}
if textField == txt5 {
txt6.becomeFirstResponder()
}
if textField == txt6{
txt6.becomeFirstResponder()
}
textField.text = string
return false
} else if ((textField.text?.count)! >= 1) && (string.count == 0) {
if textField == txt2 {
txt1.becomeFirstResponder()
}
if textField == txt3 {
txt2.becomeFirstResponder()
}
if textField == txt4 {
txt3.becomeFirstResponder()
}
if textField == txt5 {
txt4.becomeFirstResponder()
}
if textField == txt6 {
txt5.becomeFirstResponder()
}
if textField == txt1{
txt6.resignFirstResponder()
}
textField.text = ""
return false
} else if (textField.text?.count)! >= 1 {
textField.text = string
return false
}
return true
}
Happy Coding
Related Topics
How to Make One Side of a Div Pointy with CSS
How to Set Gradient Background Transparency on iOS Safari
Checking Cellular Network Type in iOS
Data Repeat in UItableview When Scrolling
How Has The Nsindexpath Initialization Changed in Swift3
Value of Type 'string' Has No Member 'stringbytrimmingcharactersinset'
Determine The Maximum Number of Characters a UIlabel Can Take
Uiviewrepresentable Automatic Size - Passing UIkit UIview Size to Swiftui
How to Make a Synchronous Request Using Alamofire
Swiftui Xcode 12.3 Can't Change Button Size in Toolbar
Fastlane Boarding with Two-Factor Authentication
Track Touch Points in Multitouch
Fetch() Doing Get Instead of Post on React-Native (iOS)
Will Download Resume After Closing My App in Background Mode
Invalid Transaction Receipt Returned by Appstorereceipturl (Nsdata), in iOS 7