An app we made utilizes keyboardWillShow notifications from NSNotificationCenter
Everything was working as expected until we tried to use the app with a device that had the Sogou custom keyboard installed. When this keyboard is used (which oddly enough it doesn't seem to always come up -- for instance secure text entries ignore the Sogou keyboard) the keyboardWillShow notification is not firing.
Does anyone know of this issue or how to disable the use of custom keyboards?
The only way I found to get this to work was to disable all custom keyboards using the app delegate:
- (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier {
if ([extensionPointIdentifier isEqualToString: UIApplicationKeyboardExtensionPointIdentifier]) {
return NO;
}
return YES;
}
Related
How do I detect that text is entered into UITextField using dictation on iOS 16?
Before iOS 16, we could detect the user is using dictation to enter text by checking primaryLanguage variable of the textInputMode being dictation. Simple enough. But this method doesn't work no more. In iOS 16, primaryLanguage is the language selected for dictation.
In my Remote Mouse & Keyboard for Mac / PC app (https://remote4mac.app), I need to detect whether the user is using dictation to wait for the whole phrase before sending it to the computer. So I started looking for alternative methods and spent a good couple of hours to no avail. But then I remembered method swizzling. iOS using UIDictationController to control dictation interactions, but it's a private class. So I had to figure out how to swizzle its methods, and fortunately, you can do that by adding a category and using category methods for it like this
#interface NSObject(PrivateSwizzleCategory)
- (void)swizzled_setupForDictationStart;
- (void)swizzled_stopDictation;
#end
#implementation NSObject(PrivateSwizzleCategory)
- (void)swizzled_setupForDictationStart {
[(NSObject *)self swizzled_setupForDictationStart];
[[NSNotificationCenter defaultCenter] postNotificationName:#"UIDictationControllerWillStart" object:nil];
}
- (void)swizzled_stopDictation {
[(NSObject *)self swizzled_stopDictation];
[[NSNotificationCenter defaultCenter] postNotificationName:#"UIDictationControllerDidStop" object:nil];
}
#end
Then you just need to run a code that will swizzle those methods with the UIDictationController like this
Method original, swizzled;
original = class_getInstanceMethod(objc_getClass("UIDictationController"), NSSelectorFromString(#"setupForDictationStart"));
swizzled = class_getInstanceMethod(objc_getClass("NSObject"), #selector(swizzled_setupForDictationStart));
method_exchangeImplementations(original, swizzled);
original = class_getInstanceMethod(objc_getClass("UIDictationController"), NSSelectorFromString(#"stopDictation"));
swizzled = class_getInstanceMethod(objc_getClass("NSObject"), #selector(swizzled_stopDictation));
method_exchangeImplementations(original, swizzled);
and you are done. Just listen to the new notifications and act accordingly in your code.
So now its time for me to see if the app will pass Apple review.
I am calling becomeFirstResponder, and showing the keyboard with it.
[inputTextField becomeFirstResponder];
However, it shows the custom keyboard that is installed on test device. Is there a way to show iOS Standard Keyboard for Number Pad?
From the answer list originally at: Prevent custom keyboard in textfield
- (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier {
if ([extensionPointIdentifier isEqualToString: UIApplicationKeyboardExtensionPointIdentifier]) {
return NO;
}
return YES;
}
I use this code in AppDelegate:
- (BOOL)accessibilityPerformMagicTap {
NSLog(#"Appdelegate = %s",__func__);
return YES;
}
When I click the home button and two-fingered double tap while using VoiceOver activates, this method is not invoked. What is the reason for this? How can I use this method with my music app?
You cannot. The method is called on the focused accessibility element while the app is foregrounded.
How to respond to starting dictation?
Known ways of responding to dictation:
dictationRecordingDidEnd - respond to the completion of the recognition of a dictated
phrase.
dictationRecognitionFailed - respond to failed dictation recognition.
Reference: UITextInput Protocol Reference
Starting in iOS 5.1, when the user chooses dictation input on a supported device, the system automatically inserts recognized phrases into the current text view. Methods in the UITextInput protocol allow your app to respond to the completion of dictation, as described in “Using Dictation.” You can use an object of the UIDictationPhrase class to obtain a string representing a phrase a user has dictated. In the case of ambiguous dictation results, a dictation phrase object provides an array containing alternative strings.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInput_Protocol/Reference/Reference.html
As far as I can tell, there's no public API for detecting when dictation has started.
If you really want to do it, and you want to be in the App Store, you can probably get away with the following approach, but it is totally unsupported, it might get you rejected anyway, and it is likely to break in a future version of iOS.
The text system posts some undocumented notifications after changing to or from the dictation “keyboard”. Two of them are posted both on a change to it and a change from it, with these names:
UIKeyboardCandidateCorrectionDidChangeNotification
UIKeyboardLayoutDidChangedNotification
Note that the second one has a strange verb conjugation. That is not a typo. (Well, it's not my typo.)
These notices are also posted at other times, so you can't just observe them and assume the dictation state has changed. You'll need to do more checking when you receive the notification. So, add yourself as an observer of one of those notifications. The first one seems less likely to go away or be renamed in the future.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(checkForDictationKeyboard:)
name:#"UIKeyboardCandidateCorrectionDidChangeNotification"
object:nil];
...
When you receive the notification, you'll want to see whether the dictation view is showing:
- (void)checkForDictationKeyboard:(NSNotification *)note {
if ([self isShowingDictationView]) {
NSLog(#"showing dictation view");
} else {
NSLog(#"not showing dictation view");
}
}
To see whether it's showing, check each window except your own application window. Normally, the only other window is the text system's window.
- (BOOL)isShowingDictationView {
for (UIWindow *window in [UIApplication sharedApplication].windows) {
if (window == self.window)
continue;
if (containsDictationView(window))
return YES;
}
return NO;
}
Recursively walk the view hierarchy checking for a view whose class name contains the string “DictationView”. The actual class name is UIDictationView but by not using the whole name you're less likely to be rejected from the App Store.
static BOOL containsDictationView(UIView *view) {
if (strstr(class_getName(view.class), "DictationView") != NULL)
return YES;
for (UIView *subview in view.subviews) {
if (containsDictationView(subview))
return YES;
}
return NO;
}
Although this question is answered, I still want to add my solution and wish to be helpful to someone else.
When you tap on the MIC button on the keyboard, primaryLanguage will change to dictation. You can detect that like this:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(handleCurrentInputModeDidChange:)
name:UITextInputCurrentInputModeDidChangeNotification
object:nil];
- (void) handleCurrentInputModeDidChange:(NSNotification *)notification
{
NSString *primaryLanguage = [UITextInputMode currentInputMode].primaryLanguage;
NSLog(#"current primaryLanguage is: %#", primaryLanguage);
}
UPDATE:
Just as what #user1686700 said, currentInputMode already been deprecated. This isn't a solution any more.
Please note - UITextInputMode's currentInputMode is deprecated as of iOS7.
We may just have to wait until Apple decides to publish the dictation API so we can make the calls that make sense as per our intentions.
I am working on Notification and my understanding on this is that IOS notifications like "textFieldShouldBeginEditing:(UITextField *)iTextField" gets posted only when you tap on a text field.
To my strange notice, my code is receiving this notification when I am tapping on "Back" button to go back to my previous view.
What are the possible chances of me getting this notification again. I believe we need not to register for such notifications. I have registered only for keyboard hide/show notifications.
Please suggest.
I found the issue. The issue was I was adding my textfield as first responder before server call and then was only removing it when you hit the return button or hit any other text field. That's why it was not getting resigned when back button was pressed. Now I have resigned it soon after server call.
Edit: I misunderstood the question. See the OP's answer.
Well, the keyboard will disappear upon navigation. It makes sense that the notification is posted in this case. One way to ignore notifications generated in response to view transitions is to keep track of your view controller's state.
- (void)viewWillDisappear:(BOOL)animated {
_transitioningView = YES;
}
- (void)viewDidDisappear:(BOOL)animated {
_transitioningView = NO;
}
- (void)viewWillAppear:(BOOL)animated {
_transitioningView = YES;
}
- (void)viewDidAppear:(BOOL)animated {
_transitioningView = NO;
}
Now, in the selector called by your keyboard notification, you can just return if the view is transitioning.
- (void)keyboardWillHide:(NSNotification*)notif {
if (_transitioningView)
return;
// Handle keyboard dismissal.
}