I'm beginning to dip my toes into Swift alongside writing UI tests and am having a problem with typing text into a textField. Below is the code:
func testLoginUsernameField() {
let app = XCUIApplication()
app.launch()
let username = "testusername2"
let usernameField = app.textFields["username_field"]
XCTAssertTrue(usernameField.exists)
usernameField.tap()
usernameField.typeText(username)
XCTAssertEqual(usernameField.value as! String, username)
}
The problem occurs when I do usernameField.typeText(username). My text continues to write tstusername2 rather than the testusername2.
This issue happens on the simulator when the Hardware Keyboard is enabled.
Disable the hardware keyboard via the menu
Go to I/O -> Keyboard -> Uncheck "Connect Hardware Keyboard" or use the shortcut ⇧⌘K.
Disable the hardware programmatically
If you'd like to disable the hardware keyboard for your Scheme, no matter what simulator you run, refer to this StackOverflow post. I attempted to use other methods to disable the hardware keyboard via the App Delegate but had no luck.
Related
i know this is a designed part of the ipad to close the keyboard when the hardware keyboard is connected (which make sense). I understand but since ipadOS 15, the virtual keyboard will hide everytime a key is pressed on the hardware keyboard. This was not the case before, you could open it back and use it. We have a prediction keyboard extension which help childrens suffering dyslexia to write. I can't seem to find any documentation on this but is there a private API or something to prevent a virtual keyboard from closing inside a keyboard extension. I can see 'Grammarly' app doing it so it is possible.
So far i can detect if a hardware keyboard is connected using the GameController SDK :
let isKeyboardConnected = GCKeyboard.coalesced != nil
This is not working too inside a keyboard extension
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return false
}
I need to see my virtual keyboard at the same time as typing with the hardware keyboard in order to see the prediction view.
Thanks
I am developing a Mac app using Swift 4 which only runs as an Agent (it only has a menu in the tray). I'm completely new to Swift.
I want it to respond to keyboard shortcuts, but the basic shortcuts (see picture) only work when the app has focus (in my case, when the menu is being clicked).
So what I need is for my app to respond to system-wide keyboard shortcuts.
I've tried:
HotKey but it doesn't seem to be compatible with Swift 4, or perhaps I can't use Swift Package Manager
this answer but again Swift 4 raises several errors that I couldn't solve
Has anyone done it?
So I ended up getting it to work with HotKey:
by using Carthage as a package manager instead of SPM
and by not using HotKey's example code from their README, which for some reason didn't work for me, but the one in their example app.
It works great!
I made a Swift package that makes this very easy:
import KeyboardShortcuts
extension KeyboardShortcuts.Name {
static let startFiveRounds = Self("startFiveRounds", default: .init(.t, modifiers: [.command, .option]))
}
#main
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
KeyboardShortcuts.onKeyUp(for: .startFiveRounds) {
// …
}
}
}
Even though you already solved your problem, I think this could be useful for other people having the same problem.
I would also recommend letting the user pick their own keyboard shortcut instead of hardcoding it. My package comes with a control where the user can set their preferred shortcut.
Is it possible to enable Bluetooth (with Discoverability on), using a button on macOS? Or enable it when an application opens? I've looked around online for a solution but everything directs towards iPhone and iOS Development rather than anything on the Mac side. Everything that is geared towards iOS also states that it is not possible on the mobile devices, but it is possible to display an alert notifying the user to turn on their Bluetooth to use their accessories. Is any alert type possible?
Edit: The closest I could get to this was opening the Bluetooth pane of the System Preferences programmatically.
It's a private API.
Add the following to your bridging header:
void IOBluetoothPreferenceSetControllerPowerState(int);
And call it with 1 to enable or 0 to disable:
func setBluetooth(on: Bool) {
IOBluetoothPreferenceSetControllerPowerState(on ? 1 : 0)
}
setBluetooth(on: true)
Don't forget to import IOBluetooth in your Swift file.
To get the current status use int IOBluetoothPreferenceGetControllerPowerState();
Starting with a blank OS X application project, I add the following code to applicationDidFinishLaunching.
func applicationDidFinishLaunching(aNotification: NSNotification) {
let app = NSApplication.sharedApplication()
guard let window = app.keyWindow else {
fatalError("No keyWindow\n")
}
print(window)
}
At launch I hit the error case because my local window variable is nil. Yet when I show the contents of the app variable, I see a valid value for _keyWindow. Also notice that the blank GUI Window is being displayed on the screen next to the stack dump.
Why does the keyWindow: NSWindow? property return nil in this case?
Thanks
NSApplication's keyWindow property will be nil whenever the app is not active, since no window is focused for keyboard events. You can't rely on it being active when it finished launching because the user may have activated some other app between when they launched your and when it finished launching, and Cocoa is designed to not steal focus.
To some extent, you may be seeing that happen more when launching from Xcode because app activation is a bit strange in that case. But, still, you must not write your applicationDidFinishLaunching() method to assume that your app is active.
What you're seeing in terms of the app's _keyWindow instance variable is, of course, an implementation detail. One can't be certain about what it signifies and you definitely should not rely on it. However, I believe it's basically the app's "latent" key window. It's the window that will be made key again when the app is activated (unless it is activated by clicking on another of its windows).
I'm really bangin' my head because I can't find the way to show the soft keyboard when there's a bluetooth input device connected to the iPad. I made some search on the web and this is the result:
a question on stackoverflow with a very short answer How can I detect if an external keyboard is present on an iPad?
an application developed by erica sadun for the cydia env http://www.tuaw.com/2010/06/02/hacksugar-bringing-back-the-on-screen-keyboard/
Erica said that the trick is to answer to the system that "There's no hardware keyboard attached".
I tried to write a category for UIKeyboardImpl and I overrided:
- (BOOL)isInHardwareKeyboardMode {
DEBUG(#"is called");
return NO;
}
But until now I haven't obtained anything. The overrided method is called but there's no soft keyboard.
Erica also said the application works by dynamic linking but I don't know how can I accomplish it. I don't need to be in the AppStore because this is a private application so I don't bother about rejection.
Thanks in advance
Ok. Finally got it. Many thanks to David, Matthias and Enrico. Here are the steps:
import the private framework GraphicsServices
call GSEventSetHardwareKeyboardAttached(NO) inside the viewDidLoad
add a button that toggles the keyboard by calling
static void toggleKeyboard(UIKeyboardImpl * keyImpl){
if (UIKeyboardAutomaticIsOnScreen()) {
UIKeyboardOrderOutAutomatic();
} else {
UIKeyboardOrderInAutomatic();
}
I've found this function on http://code.google.com/p/btstack/wiki/iPhoneKeyboardHiding
Now I can take input from the soft keyboard and from the bluetooth device at the same time.
To get around it using the apple keyboard you hit the eject key. Perhaps you can implement an action that sends the eject keycode? I think iSSH has a feature where you can tap the onscreen keyboard icon to bring it up even when a bluetooth keyboard is connected.