iOS Dev
iOS Dev

Reputation: 4248

Detect input object view type in custom keyboard extension

I am developing a custom keyboard using app extension in iOS 8.

I have a return key, and on press I want to check if input object is an UITextView, then move to next line using the following code: [self.textDocumentProxy insertText:@"\n"];. And if the input object is an UITextField, then dismiss the keyboard this way: [self dismissKeyboard];

Something like this:

- (void) returnKeyPressed
{
    if (inputObjectIsTextView)
    {
        [self.textDocumentProxy insertText:@"\n"];
    }
    else if (inputObjectIsTextField)
    {
        [self dismissKeyboard];
    }
}

The question is: How can I detect what kind of input view is currently editing?

Upvotes: 2

Views: 1900

Answers (3)

Ezimet
Ezimet

Reputation: 5078

You can`t do that , if you look at the documentation ,you see that there is no public API for detecting keyboard Extensions Text Input Object.

From The Doc. Because a custom keyboard can draw only within the primary view of its UIInputViewController object, it cannot select text. Text selection is under the control of the app that is using the keyboard. If that app provides an editing menu interface (such as for Cut, Copy, and Paste), the keyboard has no access to it

I think this also implies that you cannot access text Input object from the app that are using your keyboard extension.

Upvotes: 1

Anand Suthar
Anand Suthar

Reputation: 3798

There is no need to detect input view is a UITextField or UITextView to enter a new line

as described here under heading "API Quick Start for Custom Keyboards"

says

[self.textDocumentProxy insertText:@"\n"];   // In a text view, inserts a newline character at the insertion point

That means no need of detection, You can do in this way

- (void) returnKeyPressed
{
    [self.textDocumentProxy insertText:@"\n"];
}

This will execute if inputView is UITextView and not execute if input type is UITextField.

I have create a keyboard and test this before post it here.

Upvotes: 2

sabyasachi
sabyasachi

Reputation: 74

Try something like this, take a reference of active view as 'inputViewObject', then try

- (void) returnKeyPressed
{

    if (inputViewObject.class==[UITextView class])
    {
        [self.textDocumentProxy insertText:@"\n"];
    }
    else if (inputViewObject.class==[UITextField class])
    {
        [self dismissKeyboard];
    }
}

Hope it helps.

Upvotes: 0

Related Questions