简体   繁体   中英

How to detect when Bluetooth keyboard is disconnected from ipad

I have a UItableView that I am shrinking in size when a UIKeyboard is shown on screen, However there is the possibility that a Bluetooth keyboard can be used, I know that when a bluetooth keyboard is present UIKeyboardWillShowNotification will not be called.

So what I am doing is turning a bool on and off when the Bluetooth keyboard is connected however I am not sure how to detect when the UIKeyboard is disconnected, which is what I would like help with.

this is my code for detecting a Bluetooth keyboard.

- (void) viewdidload
//..
blueToothKeyBoardConnected = NO;
//..

- (void)UIKeyboardWillShowNotification:(NSNotification *)aNotification {

    blueToothKeyBoardConnected = YES;

}

- (BOOL)textFieldShouldBeginEditing:(UITextField*)textfield {

    // only change height if bluetoothkeyboard not present.
    if (blueToothKeyBoardConnected == YES) {
        int height = self.finishingTableView.frame.size.height;
         self.finishingTableView.frame= CGRectMake(self.finishingTableView.frame.origin.x, self.finishingTableView.frame.origin.y, self.finishingTableView.frame.size.width, 307);
    }

    //..

Effectively I would like to know when I should be setting my boolean back to NO if and when the keyboard is removed.

I had checked the height of the keyboard for detection

 -(void) keyboardWillAppear:(NSNotification *) notification
    {

        [touchView setHidden:TRUE];


        NSDictionary *userInfo = [notification userInfo];

        NSLog(@"Keyboard Dict-%@",userInfo);

        NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

        CGRect keyboardRect = [aValue CGRectValue];

        if (keyboardRect.origin.x<0 || keyboardRect.origin.x>710) {
          // connected
            return;
        }
        else {
             // disconnected
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM