Changing keyboard language in iOS programmatically in XCUI test - ios

I want to write test cases where i can change the keyboard language for each test case in a text field.
I am using XCUItest framework to write these test cases. But i am not able to change the keyboard language while running the test case.
I have gone through this link
iPhone: Change Keyboard language programmatically
and tried the below solution.
Here i am getting the return value but i am not able to use this to change the language in the text field while running the test.
class CustomTextField: UITextField {
private func getKeyboardLanguage() -> String? {
return "en" // here you can choose keyboard any way you need
}
override var textInputMode: UITextInputMode? {
if let language = getKeyboardLanguage() {
for tim in UITextInputMode.activeInputModes {
if tim.primaryLanguage!.contains(language) {
return tim
}
}
}
return super.textInputMode
}
}

Related

how do I change textField keyboard to be Arabic numbers only? Swift / UIKit

I'm trying to make my text field show only Arabic numbers keyboard, no matter what language the device have. I changed the app base language to Arabic and it is still showing me English numbers.
keyboardImage
You can create a subclass like ArabicTextField of UITextField and use textInputMode property to change your keyboard as below
class ArabicTextField: UITextField {
override var textInputMode: UITextInputMode? {
UITextInputMode.activeInputModes.filter
{ $0.primaryLanguage == "ar" }.first ?? super.textInputMode }
}

Is there any way to add a language to user system settings in iOS?

I am working on localizing my iOS application. Done with the whole process But there's an issue with the keyboard, I am providing a functionality for user to change the language from inside the app (System, English and Hindi).
However, the issue I am facing is with the keyboard. I managed to open the keyboard as per the user language but if user doesn't have Hindi added in keyboard languages in system settings it opens english keyboard.
Is there any way I can programatically add Hindi to keyboard languages in the user settings?
I am using following code in UITextField, UITextView subclass for setting keyboard langauge:
override open var textInputMode: UITextInputMode?{
let language = LanguageManager.shared.getCurrentLocale()?.languageCode ?? ""
if language == "" {
return super.textInputMode
} else {
for tim in UITextInputMode.activeInputModes {
if tim.primaryLanguage!.contains(language) {
return tim
}
}
return super.textInputMode
}
}
You can't do this. Because only user can control keyboard language. Developer can change UIKeyboardType (e.g emailAddress, numberPad).

Use localized strings in storyboard

I'm pretty new to iOS development and I was asking my self if it is possible to use localized strings from my "Localizable.strings" file directly into the storyboard.
For example in Android you can do it from the XML file like this:
android:text="#string/notConnected"
I understood that you can make a localized version of the storyboard, but having different strings files and different storyboards looks pretty ugly to me.
So is it possible to have only strings files and use what I need into the storyboard? Preferably without setting it from code?
EDIT:
This is practically what I want to do:
So is this possible? Is there a legit way to call a string from there like in Android?
I think being able to localise Strings in the storyboard is of significant advantage. I don't agree with #elk_cloner that hooking up IBOutlets for every UILabel is the way forward.
One way of getting it to work is using an #IBInspectable property on a UILabel subclass:
class LocalisableLabel: UILabel {
#IBInspectable var localisedKey: String? {
didSet {
guard let key = localisedKey else { return }
text = NSLocalizedString(key, comment: "")
}
}
}
In the storyboard set the custom class:
In the attributes inspector the localisedKey field will appear and you can just add your key here.
That's it!
EDIT:
You can localise UIButtons the same way, BUT if the text in the storyboard's title field differs from the localised String (which it will in other languages) the setting of the title will animate.
To fix this, put the setTitle in a performWithoutAnimation block:
class LocalisableButton: UIButton {
#IBInspectable var localisedKey: String? {
didSet {
guard let key = localisedKey else { return }
UIView.performWithoutAnimation {
setTitle(key.localized, for: .normal)
layoutIfNeeded()
}
}
}
}
In addition to Leon's answer, you can get rid of the need for an explicit subclass by using an extension:
extension UILabel {
#IBInspectable var localizableText: String? {
get { return text }
set(value) { text = NSLocalizedString(value!, comment: "") }
}
}
According to your requirement it's not possible but
You don't need different storyboards for localization
Suppose you want to localize a label string.
Draw and outlet and change text using
mylabel.text = nsLocalizedString("THIS_IS_MY_STRING",nil);
Of course in your localization file there will be a line.You must have different files for different language.Suppose you have a file for english and there must be a line.
"THIS_IS_MY_STRING" = "This is my string";
When you compile your app, that function will use mapping to localize your app.
Edit:
If you want detail information please have a look at these tutorials
internationalization-tutorial-for-ios-2014
and ios-localization-tutorial
There are some online script(e.g localize.py) which will help you to automatically search all of your code and find out nslocalizedString function and make lines in your localizableString files. like this.
"THIS_IS_MY_STRING" = "THIS_IS_MY_STRING"
and later on you just have to write actual string there. :)
Make a button class and set #IBInspectable attribute to the button class
class Button: UIButton {
#IBInspectable public var referenceText: String = "" {
didSet {
self.setTitle(NSLocalizedString(referenceText, comment: ""), for: .normal)
}
}
}
Then in the storyboard you can set referenceText
The button text will be "Sign In"
Another possible way is to localize the storyboard and simply change the values for labels and buttons directly on the .string file.
First select the storyboard, and click on localize:
Then you'll be able to select the languages you want.
This way you'll be able to continue developing on your language of choice and simply edit the .string file that is generated
for button
extension UIButton {
#IBInspectable var localizableText: String? {
get { return titleLabel?.text }
set(value)
{
setTitle(NSLocalizedString(value!, comment: ""), for: .normal)
}}}

How to disable auto-complete when running Xcode UI Tests?

As part of my UI Tests, I'm generating a random string as titles for my objects. The problem is that when this title is input via a keyboard (using XCUIElement.typeText()), iOS sometimes accepts an auto-suggested value instead.
For example, I may want it to type an auto generated string of "calg", but auto correct will choose "calf" instead. When I try to look for this value later on with an assertion, it doesn't exist and fails incorrectly.
Is there a way to tell the UI tests that they shouldn't be using auto correct, or are there an workarounds I can use?
Unless you need auto-suggest for any test scenarios, did you try turning off auto-correction in device/simulator settings.
Settings-> General -> Keyboard -> Auto-Correction
I don't believe you can turn off auto-correction through code from your UI Testing target.
You can, however, turn it off for the individual text view from your production code. To make sure auto-correction is still on when running and shipping the app, one solution would be to subclass UITextField and switch on an environment variable.
First set up your UI Test to set the launchEnvironment property on XCUIApplication.
class UITests: XCTestCase {
let app = XCUIApplication()
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launchEnvironment = ["AutoCorrection": "Disabled"]
app.launch()
}
func testAutoCorrection() {
app.textFields.element.tap()
// type your text
}
}
Then subclass (and use) UITextField to look for this value in the process's environment dictionary. If it's set, turn auto-correction off. If not, just call through to super.
class TestableTextField: UITextField {
override var autocorrectionType: UITextAutocorrectionType {
get {
if NSProcessInfo.processInfo().environment["AutoCorrection"] == "Disabled" {
return UITextAutocorrectionType.No
} else {
return super.autocorrectionType
}
}
set {
super.autocorrectionType = newValue
}
}
}
Here is how I disabled it in my UI Test
app.textFields.element(boundBy: 0).tap()
let keyboards = app.keyboards.count
XCTAssert(keyboards > 0, "You need enable the keyboard in the simulator.")
app.buttons["Next keyboard"].press(forDuration: 2.1)
let predictiveOn = app.switches["Predictive"].value as! String == "1"
if predictiveOn {
app.switches["Predictive"].tap()
} else {
app.buttons["Next keyboard"].tap()
}
app.buttons["Next keyboard"].press(forDuration: 2.1)
let predictiveOff = app.switches["Predictive"].value as! String == "0"
XCTAssert(predictiveOff, "Predictive mode is not disabled")
app.buttons["Next keyboard"].tap()

How to restrict Special Characters in iOS by using xamarin?

I am working with IOS by using xamarin ,I am new to technology I have one requirement ,That is I don't want to allow any special character in my text box .How can I do this any one help me
thanks
What you can do is use ShouldChangeCharacters delegate.
Using it you can decide weather or not to update the text of the UITextField.
For example lets say you have a UITextField named textField:
textField.ShouldChangeCharacters = (textField, range, replacementString) => {
if (isSpecialCharacter) {
return false;
} else {
return true;
}
};

Resources