How to enter the text in search bar using UIAutomation? - ui-automation

Could you please help me out in entering the text in search bar using UIAutomation scripts for IPhone Application.
Thanks in advance,
Madhu.

UIASearchBar inherits from UIATextField:
http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Reference/UIASearchBarClassReference/UIASearchBar/UIASearchBar.html#//apple_ref/doc/uid/TP40009913
Try the "setValue" method:
searchBar.setValue("search for this");

Since through tap input method you communicate with the device, you can try user-like type of action: tapping.
To bring up the keyboard you just tap on the text input field:
someTextInputField.tap();
But be aware of the fact that there is no keyboard object instance available for actions if you do not activate the keyboard itself by tapping on any text input field.
Once the keyboard is activated:
var App = UIATarget.localTarget().frontMostApp();
var Keyboard = App.keyboard();
var Keys = Keyboard.keys();
var AltKeys = Keyboard.buttons();
Where AltKeys represent service buttons like Shift, Done, etc.
And Keys represent all other buttons.
P.S. Keep in mind that you have to switch between keyboard tabs to get access to either characters, numbers, special characters.

Related

How to enable "Find next" when Find bar within NSTextView is non-empty and has focus

I've got a simple, document-based, text-viewer MacOS app working. Each window's view has nothing more than a MyTextView (a subclassed NSTextView) in it. MyTextView has only two methods in it, in order to see what's happening with the Find bar and Find-related menu items:
class MyTextView : NSTextView
{
override func performFindPanelAction(_ sender: Any?)
{
let menuItem = sender as! NSMenuItem
Swift.print("performFindPanelAction: tag = \(menuItem.tag)")
if menuItem.tag == 2 { // "Find next" menu command tag
}
super.performFindPanelAction(sender)
}
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool
{
Swift.print("validateMenuItem: tag = \(menuItem.tag)")
return super.validateMenuItem(menuItem)
}
}
So when the user types Command-F (keyboard equivalent for the "Find ..." command, with tag = 1), the Find bar pops open at the top of the scrolling MyTextView with focus changing to its search text entry field. And the terminal shows:
validateMenuItem: tag = 0
validateMenuItem: tag = 1
performFindPanelAction: tag = 1
So far so good. The user types in a search text, and hits Return. Nothing prints on the terminal (i.e., performFindPanelAction() is not called), but the search occurs and the text view scrolls to show the first match, even though focus remains set for the search text entry field in the Find bar. So tapping Return again and again finds subsequent matches, without any interaction with the overriding performFindPanelAction() method in my subclass.
The problem is that instead of repeatedly tapping Return, the user should be able to use the entirely equivalent "Find Next" command. But when the Find bar has focus in its search text entry field, the default Find menu's "Find Next" command is inexplicably disabled. So typing Command-G (the keyboard equivalent for "Find Next") just makes a sound to indicate that it is disabled and does nothing. This is confusing to the user, because it makes entire sense for the "Find Next" command to be enabled if the search text entry field is non-empty.
Clicking the mouse on the text in the MyTextView changes focus back to it, and suddenly the Find Next (and Find Previous) (with tags 2 and 3) commands are enabled by the system, and they work. The terminal prints out:
validateMenuItem: tag = 2
performFindPanelAction: tag = 2
for each time Command-G is typed. This continues to work (as it should) even after the Find bar has been hidden by clicking in its Done button.
I want Command-G (the "Find Next" command) to initiate a search in MyTextView as well as continue the search. No one wants to search the Find bar's own search text entry field, after all.
Question: How can I override the default system behavior for a Find bar, so that the Find Next (and Find Previous) commands in the Find sub-menu are enabled and work when the Find bar's search text entry field still has focus and is non-empty?
Per Willeke's answer, the substitution of newer performTextFinderAction() in place of the older performFindPanelAction() is a hint to the solution, which I finally got working, but not really for that reason. Xcode makes getting there really confusing though, so here's what I think works and the steps I took to get there.
MyTextView is a potential first responder. In it, the disfavored performFindPanelAction() is an override method. Hence, in the storyboard, Cmd-Clicking "First Responder" shows this method name amongst so many other possibilities for a FirstResponder. This method is what the Xcode's outdated template has wired the Find Next and Find Previous menu item commands' action outlet to.
But after converting override func performFindPanelAction() to override func performTextFinderAction(), the name of the new method still didn't show up in the list when Cmd-Clicking on FirstResponder. There was nothing to (re-)wire the menu item's action outlets to.
So I inserted #IBAction in front of the override func performTextFinderAction(). This caused the method name to appear in the list and I was able to (re-)wire the Find menu commands' action outlets to the newer method. This enabled Find Next and Find Previous when the search text entry field had focus and non-empty text. Problem solved.
But curiously, I then deleted the `IBAction' modifier, and everything still works.
Even more curiously, I then deleted the entire performTextFinderAction() method in MyTextView, and everything still works. So the Menu Items in the storyboard now have the proper name of the action to call, and the system's new default behavior fixes the problem, so there's no need to override anything in MyTextView.
The culprit is Xcode 13.3's outdated template, I guess.
In Mac OS X 10.7 performFindPanelAction: is replaced by performTextFinderAction:. The documentation of performTextFinderAction: states:
Before OS X v10.7, the default action for these menu items was performFindPanelAction:. Whenever possible which you should update your implementation to use this new action.
The Find Panel uses the "new" performTextFinderAction: but apparently Apple forgot to update the Xcode templates. Add the performTextFinderAction: action to the first responder and replace the action of the menu items to make Find Next/Prev work.

Handling key events in OS X with Swift and Cocoa

I have a very basic OS X that has a few different elements.
A text field, a table view and a file contents view.
I have a single ViewController.
I'd like to be able to intercept specific key events for each of these elements in the storyboard in my ViewController and change the focus between the different elements.
For example, if the cursor is currently in the file contents view, and I hit ESCAPE, I'd like for the focus to be transferred to the text field.
Or, if the focus is currently on the table view and I hit ENTER, that the cursor/focus is moved to the file contents view.
What's the best way to handle this?
I have tried overriding the keyDown method in the ViewController but with things such as autocomplete getting in the way, I'm not having much luck. I have added a print statement to keyDown to check if the function is receiving events, but it's not always fired.
Update
Except for the specific keys that I want to intercept, I want all other key events to behave as normal. For example, typing in the file contents view, or the text field.
I would highly recommend watching the WWDC talk #145 from 2010, Key Event Handling in Cocoa Applications. It gives an overview of the event delivery mechanism, and several ways of handling events:
Make a menu item or button whose keyEquivalent is the escape key.
Override cancelOperation(_:) or complete(_:), which are the two NSResponder methods which can get invoked by default when the escape key is pressed. (There are other methods for the enter/return key.)
Override sendEvent(_:) in NSApplication or NSWindow to intercept all events and bypass the default behavior.

UITextField keyboard return button title

I wanted to change the name of the return button key as I'm not developing the app in English. I'm having an UIAlertController and added a textField (.addTextFieldWithConfigurationHandler { (textField) -> Void in <someCode> }). How can I now change the title of the return button key in Swift? Actually there shouldn't be any difference to a normal UITextField. Thank for any help :]
You cannot set custom text for the return key on iOS. There are a number of options but those are the only ones.
If you are only worried about translation, the keys will be translated automatically depending on the language that is configured on the users device. Otherwise, you are going to have to create an entirely custom keyboard (which is almost certainly not worth the effort).

iphone - apns alert option (no view option) possible?

In the official Apple document it says
alert
string or
dictionary
If this property is included, iOS displays a standard alert. You may specify a string as the value of alert or a dictionary as its value. If you specify a string, it becomes the message text of an alert with two buttons: Close and View. If the user taps View, the application is launched.
Alternatively, you can specify a dictionary as the value of alert. See Table 3-2 for descriptions of the keys of this dictionary.
But I wonder if there's any option that I can add to the code to make the push only show "close" button.
Ex of what I have now:
#"{\"aps\":{\"alert\":\"Update OS\"},\"acme1\":\"updateos\",\"acme2\":42}";
I think you can use a dictionary. Have a look at Table 3-2 on the same page, for the key action-loc-key, it says:
If a string is specified, displays an alert with two buttons, whose behavior is described in Table 3-1. However, iOS uses the string as a key to get a localized string in the current localization to use for the right button’s title instead of “View”. If the value is null, the system displays an alert with a single OK button that simply dismisses the alert when tapped. See “Localized Formatted Strings” for more information.
The single OK button is perhaps what you need.

iOS keyboard with "Go" button instead of return

Is there a way to use the keyboard with "Go" key instead of the "Return" key like when you are accessing login fields in Safari? I'm guessing this should be something trivial but my searches aren't turning up anything. :(
Thanks in advance.
For a UITextField or editable UITextView:
[myTextField setReturnKeyType:UIReturnKeyGo];
You can also configure this in Interface Builder, under Text Input Traits for your text field/view:
In Xamarin.iOS or Monotouch is:
myTextField.ReturnKeyType = UIReturnKeyType.Go;
Possible values are:
Default
Go
Google
Join
Next
Route
Search
Send
Yahoo
Done
EmergencyCall
Continue
myTextField.returnKeyType = UIReturnKeyType.Go
Other options for return key name can be found at: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/#//apple_ref/c/tdef/UIReturnKeyType
You can use this simple way: [textField setReturnKeyType:UIReturnKeyGo]; instead of "Add" button.
Other, you create a programmatically keyboard with buttons which you want.