11

I have a bluetooth barcode device. If connect the bluetooth device to the iPhone, I can't write anything using iPhone keyboard. you already know that IPhone keyboard does not show on, because the bluetooth device is recognized keyboard.

But!!! I have to write something by keyboard into the textbox while iphone connect with bluetooth device.

Please Let me know how to do that! :) Thanks~

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2809312
  • 165
  • 2
  • 11

2 Answers2

13

We can show device virtual keyboard even when a bluetooth keyboard is connected. We need to use inputAccessoryView for that.

We need to add below code in app delegate.h

@property (strong, nonatomic) UIView *inputAccessoryView;

add below notifications in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method in delegate.m

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBegan:) name:UITextFieldTextDidBeginEditingNotification object:nil];

This will call below method when we focus on a textField.

//This function responds to all `textFieldBegan` editing
// we need to add an accessory view and use that to force the keyboards frame
// this way the keyboard appears when the bluetooth keyboard is attached.
-(void) textFieldBegan: (NSNotification *) theNotification
{

        UITextField *theTextField = [theNotification object];

        if (!inputAccessoryView) {
            inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
            [inputAccessoryView setBackgroundColor:[UIColor lightGrayColor]];
        }

        theTextField.inputAccessoryView = inputAccessoryView;

        [self performSelector:@selector(forceKeyboard) withObject:nil afterDelay:0];
}

and the code for "forceKeyboard" is,

-(void) forceKeyboard
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    inputAccessoryView.superview.frame = CGRectMake(0, 420, screenHeight, 352);

}

This works fine for us. We use a hidden text field for getting input from bluetooth keyboard and for all other text fields we use device virtual keyboard which is displayed using inputAccessoryView.

Please let me know if this helps and if you need any more details.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Shyju
  • 208
  • 3
  • 9
  • 3
    this doesn't work on iOS8 & 9 , is there any alternative solution ? – Sanju Feb 16 '16 at 09:16
  • Anybody found a solution to this? – Andy M Apr 12 '16 at 20:58
  • @AndyM , does it show any error/warning when you implement the above code in iOS 8/9 ? – HardikDG Apr 16 '16 at 11:21
  • 1
    Did anyone find a solution yet? iOS 10 and still the same problem. Now it only shows the inputAccessoryView. I thought about set up a button to force the keyboard to show up, but I don't know how or if it's even possible – barbarity Jan 01 '17 at 16:00
0

Create a UIView Subclass follow UIKeyInput protocol.

@interface SomeInputView : UIView <UIKeyInput> {

and in implementation file (.m)

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)insertText:(NSString *)text {
    //Some text entered by user
}

-(void)deleteBackward {
    //Delete key pressed
}

Whenever you want to show Keyboard just do

[myInputView becomeFirstResponder];
Matthew
  • 9,851
  • 4
  • 46
  • 77
iOS77
  • 522
  • 5
  • 3