2013-10-17 38 views
10

我有一個藍牙條形碼設備。 如果將藍牙設備連接到iPhone,我無法使用iPhone鍵盤輸入任何內容。 你已經知道iPhone鍵盤沒有顯示,因爲藍牙設備是被識別的鍵盤。我想用藍牙設備強制鍵盤

但是!!!當iPhone連接藍牙設備時,我必須通過鍵盤將文字寫入文本框。

請讓我知道該怎麼做! :) 謝謝〜

+0

你找到任何解決方案?這裏同樣的問題! – kokemomuke

回答

12

即使連接了藍牙鍵盤,我們也可以顯示設備虛擬鍵盤。我們需要使用inputAccessoryView

我們需要添加以下代碼的應用程序delegate.h

@property (strong, nonatomic) UIView *inputAccessoryView; 

在下面添加通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法delegate.m

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

這將調用下面的方法時,我們關注在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]; 
} 

和 「forceKeyboard」 的代碼,

-(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); 

} 

這工作得很好了我們。我們使用隱藏文本字段獲取來自藍牙鍵盤的輸入,以及我們使用設備虛擬鍵盤的所有其他文本字段,該設備虛擬鍵盤使用inputAccessoryView顯示。

請讓我知道這是否有幫助,如果您需要任何更多的細節。

+4

不幸的是,這不工作IOS8 – fabrizotus

+3

這不適用於iOS8和9,有沒有其他解決方案? – Sanju

+0

有人找到了解決辦法嗎? –

0

按照UIKeyInput協議創建一個UIView子類。

@interface SomeInputView : UIView <UIKeyInput> { 

和實現文件(.M)

-(BOOL)canBecomeFirstResponder { 
    return YES; 
} 

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

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

每當你想顯示的鍵盤只是做