Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth Keyboard does not work on Flutter webview. (iOS + Hardware Keyboard)

Tags:

ios

flutter

We have developed an application in which we need to open a WebView to show the user a text editor. After the user interacts with the editor, we need to get some data from the URL.

To show the WebView, we use the package webview_flutter: ^2.0.8 and the problem is that some users want to connect their Bluetooth keyboard to their devices and the webview section does not recognize the Bluetooth keyboard on iOS.

First, we believed that it was due to some permission with the Bluetooth, but apparently that was not the case, because we added those permissions and nothing change, apart from that, the text fields and text editors works perfectly in others sections of the app, the problem is just on the webview running on iOS.

Due that we need the URL data, I followed the steps described in this link. Then I opened the webview as a modal in a similar way that it was done in this example, but anyway it does not recognize the keyboard.

The next thing I tried was to wrap the FlutterViewController in a UINavigationController, so that I can navigate to the WebViewController that I created, but when this line is executed:

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController

This error is triggered:

Could not cast value of type 'UINavigationController' (0x11732c8f0) to 'FlutterViewController' (0x111370520).

I am very new in Swift/iOS so I don't know how to solve this problem.

Is there a way to instantiate or get the BinaryMessenger of the FlutterViewController to create the FlutterMethodChannel?

Do you know a package that open the webview in a different way? Because I have also tried other packages, but apparently all of them do the same as webview_flutter does, that is, they use UiKitView that does not work for me for this problem.

Here a repo, where I try to solve this: https://github.com/jorgesarabia/flutter_hc_webview_example

Thank you!

like image 649
J. Sarabia Avatar asked Jan 24 '23 08:01

J. Sarabia


1 Answers

In this PR github bug was fixed, but it's not in release.

You can use this temporary solution (in your IOS project file ios/Runner/AppDelegate.swift):

extension FlutterViewController {
    open override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesBegan(presses, with: event)
    }
    
    open override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesEnded(presses, with: event)
    }
    
    open override func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesCancelled(presses, with: event)
    }
}
like image 96
grebennikovlmax Avatar answered May 20 '23 02:05

grebennikovlmax