Dean Davids
Dean Davids

Reputation: 4214

Prevent custom keyboard in textfield

I was experimenting with how a custom keyboard affects my app. I installed Swype on my iPhone 6.

I find that in some of my views where I have custom inputView property set on a text field, the Swype keyboard is overriding and presenting instead of my picker. This completely breaks my UI and cannot be allowed.

Is there a way to explicitly tell iOS 8 only to use the inputView I have set?

Is this a bug, perhaps? It is not at all expected behavior to allow a third party to override my input spec?

Upvotes: 18

Views: 4701

Answers (5)

Mr. 0xCa7
Mr. 0xCa7

Reputation: 140

To stop users from using custom keyboard extensions for individual fields, you can use SecureTextEntry in UIKit (SecureField in SwiftUI). Although these objects automatically mask user input without the option to change it (which makes them simple to use for passwords, not applicable for all kinds of secrets).

For example, the SwiftUI declaration looks as follows:

var passw: String = ""
(...)
SecureField("Password", text: $passw)

The first argument is a prompt that the user will see.

Note that SecureField will automatically block keyboard extension not only on its field but also on other fields on the same application screen (applies to TextField as well).

Upvotes: 0

Gerard G
Gerard G

Reputation: 3463

Swift 4

  func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplicationExtensionPointIdentifier) -> Bool {
        if (extensionPointIdentifier == .keyboard) {
            return false
        }
        return true
    }

Upvotes: 6

Craig Pearlman
Craig Pearlman

Reputation: 1926

Using the answer from Pablo Ezequiel Romero as a starting point, I was able to get things to work for me. Essentially, rather than using a UIViewController for the custom keyboard, use a UIInputViewController and put your controls inside the UIInputViewController's inputView. Then, assign the inputView of your UITextField or UITextView to the inputView of the UIInputViewController.

If you're using auto layout, you need to make sure that you set everything properly and make sure to set an initial height constraint on the inputView and set its priority below the max 999 level (I used 800). Any height will do; the system will replace your constraint with one of its own. The lower priority avoids auto layout conflicts. (For me, if I didn't include this constraint, the final view wouldn't have any height at all.)

When I did all this, I was able to switch in and out of my (internal to the app) custom keyboard and any third-party keyboard extension.

Upvotes: 4

Pablo Romero
Pablo Romero

Reputation: 91

I had a similar issue and I was able to fix it using UIInputViewController. Basically the view that I set in inputView is the view of my UIInputViewController subclass. I was using UIViewController, but after replacing the base view controller it started to work well.

Upvotes: 2

eertl
eertl

Reputation: 371

You can disable custom keyboard for your app with the following code:

include this in your app delegate:

- (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier {
    if ([extensionPointIdentifier isEqualToString: UIApplicationKeyboardExtensionPointIdentifier]) {
        return NO;
    }
    return YES;
}

Upvotes: 9

Related Questions