Can't find any example of using SwiftUI in Keyboard Extension.
I create an extension and trying to create simple SwiftUI Button with no action (it just prints debug text). But there is no visible button in Keyboard.
Is it possible to create SwiftUI custom keyboard?
struct SwiftUIButton: View{
let action: () -> ()
var body: some View{
Button(action: action){Text("Tap me")}
}
}
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton!
//1.insert this: SwiftUIButton is a simple Button View
var swiftUIButtonView: SwiftUIButton!
//...
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
//2. try to insert my SwiftUI View
let swiftUIButtonView = SwiftUIButton(action: {print("test")})
let vc = UIHostingController(rootView: swiftUIButtonView)
//I tried that with no success
//guard let inputView = inputView else { return }
//inputView.addSubview(vc.view)
self.view.addSubview(vc.view)
//all that following code is standard from Xcode
self.nextKeyboardButton = UIButton(type: .system)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
self.view.addSubview(self.nextKeyboardButton)
self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
When I try to test it in simulator, I see empty Keyboard
and some errors in debug console:
2020-01-23 02:07:13.421876+0300 SwiftUIKeyboard[4723:376225] Failed to inherit CoreMedia permissions from 4717: (null) 2020-01-23
02:07:13.460713+0300 SwiftUIKeyboard[4723:375598] [External] -[UIInputViewController needsInputModeSwitchKey] was called before a connection was established to the host application. This will produce an inaccurate result. Please make sure to call this after your primary view controller has been initialized.
last message repeats 6 times.
What am I doing wrong? Or do I need to create UIKit Keyboard View and implement SwiftUI inside of it?

SwiftUI. So I don't know how doesUIKitworks:( Is there some simple instructions how to implementSwiftUIViewtoUIKitinUIInputViewController? Or I have to start it over again and learnUIKit?[UIInputViewController needsInputModeSwitchKey] was called...