how to detect Number and Decimal Keyboard iOS - ios

I'm developing a custom keyboard and based on Apple App Store Review guideline I have to provide a keypad for decimal and number pad :
Keyboard extensions must provide Number and Decimal keyboard types as described in the App Extension Programming Guide or they will be rejected
So I have to provide a keyboard for Decimal and Number Textfield, so how can I detect textfield type.
There is nothing about Number or Decimal Keyboard in App Extension Programming Guide so How detect which textfield I should load Decimal Keyboard or Number Keypad ?

You can get the current keyboard type before your view appears like so (in Swift).
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Setup keyboard manager to correct type
if let inputTraits = self.textDocumentProxy as? UITextInputTraits {
let keyboardType = inputTraits.keyboardType
let returnKeyType = inputTraits.returnKeyType
}
}
This from my project but reformatted for clarity. I also didn't compile it since it's such a simple example.
I've found it's good not to trust any extension API values until the view is visible or about to appear, but you could also try this in viewDidLoad()

Related

Detecting Key Press/ Key Down iOS [duplicate]

This question already has an answer here:
Key up and key down enabled on ios UIKeyboard [closed]
(1 answer)
Closed 5 months ago.
I am developing a library, which will be used in an app,
where I need to know the timestamp of key pressed (key down and key up moments), in the iOS(iPhone) virtual keyboard.
I was trying to do it by inheritance from UIViewController and override pressesBegan and pressesEnded functions like here, but it turns out its only working for external physical keyboards (so like >1% users of iPhones?)
I was trying to do this by getting all the textFields:
func getAllTextFields(fromView view: UIView)-> [UITextField] {
return view.subviews.flatMap { (view) -> [UITextField] in
if view is UITextField {
return [(view as! UITextField)]
} else {
return getAllTextFields(fromView: view)
}
}.flatMap({$0})
}
and use of .addTarget method but there's no function which provide information about "key down" moment:
textFields.forEach{($0.addTarget(self, action: #selector(textFieldEditingBegin),
for: .editingDidBegin))}
textFields.forEach{($0.addTarget(self, action: #selector(textFieldDidChange),
for: .editingChanged))}
textFields.forEach{($0.addTarget(self, action: #selector(textFieldDidEnd),
for: .editingDidEnd))}
The .editingChanged method can only be translated to "key up".
The other ones, tells us about whole textField, so the moment when user "tap" on it and not the moment when he "tap" the letter in the virtual keyboard.
I have not found anything useful for me in the NotificationCenter, UIResponder or UITextFieldDelegate.
So im asking if anyone knows how can I retrieve such information from a iOS keyboard,
im not very familiar with Extension mechanism and maybe there is some other stuff im able to do in my scenario.
Thanks.
You can detect user's tap using UITextFieldDelegate
func textFieldDidBeginEditing(_ textField: UITextField) // became first responder

NumberPad for iPad?

I set Number Pad for my TextField on the Storyboard, because I need only numbers for my app. But it works for iPhone only. I can't find solution for iPad. I tried
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.keyboardType = .numberPad
but it doesn't work. Help me please.
iPad doesn't have number only keyboard. You should implement your own. There are some git projects you can use like these:
- Some Swift Repositories:
NumericKeyboard
NumPad
NumberPad
KBNumberPad
- Some Objective-c Repositories:
HSNumericField
NumericKeypad
NumberPad

Numeric keyboard with decimal

I´m building an app with equations and I have used a numeric keyboard but I want to have a decimal slot on it. The default is only numbers. How do you add a decimal input on that keyboard?
This is the code I use to get the numeric keyboard
.keyboardType = UIKeyboardType.NumberPad
Simply change the keyboard type:
self.someTextField.keyboardType = UIKeyboardType.decimalPad
See the documentation for all the keyboard types.
Select Decimal Pad in Keyboard Type for Text Field in Attribute Inspector.
Refer the image below for more detail
In storyboard go to Attribute Inspector. There you can change keyboard type.You can check here
set keyboard type to textField in swift 3
#IBOutlet weak var txtNumber: UITextField!
override func viewDidLoad(){
self.txtNumber.keyboardType = .decimalPad
}

How can I add a Number and Decimal keyboard into my Custom Keyboard on iOS8?

I created a custom keyboard in Swift but it was rejected from the App Store because: "keyboard extension does not include Number and Decimal types".
How can I add these two keyboards easily?
I tried to rebuild the original view but it doesn't work properly. I'm sure there is a solution to create 2 or 3 different views and switch between them.
How can I switch between keyboard types when the keyboard type changes?
You can detect changes to the keyboard type in textDidChange. You need to get the UITextDocumentProxy then detect the proxy's keyboardType, and then you can do whatever layout changes needed.
override func textDidChange(_ textInput: UITextInput?) {
// Called when the document context is changed - theme or keyboard type changes
let proxy = self.textDocumentProxy as UITextDocumentProxy
if proxy.keyboardType == UIKeyboardType.numberPad
|| proxy.keyboardType == UIKeyboardType.decimalPad {
//add code here to display number/decimal input keyboard
}
}

IOS Dismiss/Show keyboard without Resigning First Responder

My application is used with a barcode scanner connected via Bluetooth. When the scanner is connected you can double tap a button on the scanner to dismiss/show the on screen keyboard. 90% of the time the user will want the keyboard to be hidden as they will be scanning a barcode to input data. There are a few exceptions that I know of ahead of time that the keyboard will need to be enabled, I would like to save them the effort of pressing the scanner button to bring up the keyboard and instead force the keyboard to show up.
The scanner does not use resignfirstresponder to dismiss the keyboard, this is evident because the cursor is still visible and scanning a barcode will input data into the current text field.
Does anyone know how to dismiss/show the on screen keyboard without using resignfirstresponder?
For reference I am using this scanner http://ww1.socketmobile.com/products/bluetooth-scanners/how-to-buy/details.aspx?sku=CX2864-1336
To end editing completely in the view you can use the following
[self.view endEditing:YES];
This will remove the keyboard for you in the view.
For anyone still struggling with this, you can achieve this in Swift by making the inputView of the textfield equals UIView()
That is:
yourtextfield.inputview = UIView()
I ran into this today and have found a solution. The trick is to use a secondary text field that is off-screen or hidden with a custom empty inputView set and make that field become the first responder. That field captures text from the hardware scanner while the software keyboard hides.
However I got this working using a very similar approach and instead making the view controller itself the first responder as the scanning input view.
Example:
class SearchViewController: UIViewController, UIKeyInput {
let searchField = UITextField()
let softwareKeyboardHider = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(searchField)
inputAssistantItem.leadingBarButtonGroups = []
inputAssistantItem.trailingBarButtonGroups = []
}
override var canBecomeFirstResponder: Bool {
return true
}
override var inputView: UIView? {
return softwareKeyboardHider
}
var hasText: Bool {
return searchField.hasText
}
func insertText(_ text: String) {
searchField.insertText(text)
}
func deleteBackward() {
searchField.deleteBackward()
}
}
Now, when you want to hide the software keyboard make SearchViewController the first responder.
To show the software keyboard make SearchViewController.searchField the first responder.

Resources