How does Google's custom iOS keyboard, Gboard, programmatically dismiss the frontmost app?
Your guess is correct — Gboard is using private API to do it.
… though not through exploring view hierarchy or event injection.
When the voice-to-text action is done, we can check the syslog from Xcode or Console that it calls the -[AVAudioSession setActive:withOptions:error:]
method. So I've reverse-engineered the Gboard app and look for the stack trace related to this.
Climbing up the call stack we can find the -[GKBVoiceRecognitionViewController navigateBackToPreviousApp]
method, and…
…_systemNavigationAction
? Yep, definitely private API.
Since class_getInstanceVariable
is a public API and "_systemNavigationAction"
is a string literal, the automatic checker is not able to note the private API usage, and the human reviewers probably don't see anything wrong with the "jump back to the previous app" behavior. Or probably because they are Google and you are not…
The actual code that performs the "jump back to previous app" action is like this:
@import UIKit;
@import ObjectiveC.runtime;
@interface UISystemNavigationAction : NSObject
@property(nonatomic, readonly, nonnull) NSArray<NSNumber*>* destinations;
-(BOOL)sendResponseForDestination:(NSUInteger)destination;
@end
inline BOOL jumpBackToPreviousApp() {
Ivar sysNavIvar = class_getInstanceVariable(UIApplication.class, "_systemNavigationAction");
UIApplication* app = UIApplication.sharedApplication;
UISystemNavigationAction* action = object_getIvar(app, sysNavIvar);
if (!action) {
return NO;
}
NSUInteger destination = action.destinations.firstObject.unsignedIntegerValue;
return [action sendResponseForDestination:destination];
}
In particular, the -sendResponseForDestination:
method performs the actual "go back" action.
(Since the API is undocumented, Gboard is actually using the API incorrectly. They used the wrong signature -(void)sendResponseForDestination:(id)destination
. But it happens that all numbers other than 1
will work the same, so the Google developers are lucky this time)
Is a completely custom keyboard, for my app only, acceptable?
Totally acceptable inside your app! and there should be no acceptance criteria for that!
You can even make a custom keyboard to use outside your app (I developed two myself), however there are acceptance criteria for that (like having a button to switch to another keyboard).
Hope that helps!
Related Topics
Behavior Differences Between Performblock: and Performblockandwait:
--Resource-Rules Has Been Deprecated in MAC Os X >= 10.10
Reading Long Characteristic Values Using Corebluetooth
How to Check If a Uialertcontroller Is Already Presenting
Right Aligned Uitextfield Spacebar Does Not Advance Cursor in iOS 7
Adding Glow Effect to Uibutton - iOS
Setting Maximum Number of Characters of 'Uitextview ' and 'Uitextfield '
Why Does My Uitableview "Jump" When Inserting or Removing a Row
Create Uicollectionviewcell Subclass with Xib
How to Animate Tab Bar Tab Switch with a Crossdissolve Slide Transition
Uitextfield - Capture Return Button Event
Change Uidatepicker Font Color
Saving Coredata To-Many Relationships in Swift
What Do Horizontalaccuracy and Verticalaccuracy of a Cllocation Refer To