简体   繁体   English

EA showBluetoothAccessoryPicker 未在 SwiftUI 中显示

[英]EA showBluetoothAccessoryPicker not showing in SwiftUI

I have created a small program that needs to connect to an external accessory.我创建了一个需要连接到外部附件的小程序。 I have been able to do so successfully with UIKit and the EA framework.我已经能够使用 UIKit 和 EA 框架成功地做到这一点。

The problem I am having is that I have a SwiftUI based app that needs to use the External Accessory but when I call the showBluetoothAccessoryPicker function, the picker window does not show.我遇到的问题是我有一个需要使用外部附件的基于 SwiftUI 的应用程序,但是当我调用 showBluetoothAccessoryPicker 函数时,选择器窗口不显示。

What I have tried so far:到目前为止我已经尝试过:

I have read that you need to have the app delegate with我读过你需要有应用程序委托

var window: UIWin

setup correctly to see the picker window.正确设置以查看选择器窗口。 So I created a class所以我创建了一个类

class AppDelegate: UIApplicationDelegate
{
    var window: UIWindow?
}

I then associated the app delegate with my swiftui "App" struct like this:然后,我将应用程序委托与我的 swiftui“App”结构相关联,如下所示:

@main
struct POC: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            TabSelectionView()
        }
    }
}

Nevertheless, when I call showBluetoothAccessoryPicker the picker window does not show.不过,当我调用showBluetoothAccessoryPicker时,选择器窗口不会显示。

I see the following in the output log:我在输出日志中看到以下内容:

2021-09-20 13:16:31.804796-0500 POC[2150:1514447] IAPDHasLaunched: kIAPAvailableNotification iapdAvailableState 0 -> 0
2021-09-20 13:16:31.805791-0500 POC[2150:1514447] IAP2DHasLaunched: kIAP2AvailableNotification iap2dAvailableState 0 -> 0
2021-09-20 13:16:31.807219-0500 POC[2150:1514447] -[EAAccessoryManager _initFromSingletonCreationMethod] isRunningOnMac

This log appears correct and matches the output from a working demo app that uses UIKit.此日志显示正确,并且与使用 UIKit 的工作演示应用程序的输出相匹配。 It's just that the picker window does not show.只是选择器窗口没有显示。

Does anybody know what needs to be done to get the picker window to appear correctly for SwiftUI apps?有人知道需要做什么才能让 SwiftUI 应用程序的选择器窗口正确显示吗? Is there any SwiftUI examples of connecting to an External Accessory that anybody could point me towards?是否有任何连接到外部附件的SwiftUI示例,任何人都可以指向我?

Thanks in advance for any help.提前感谢您的帮助。

It's just broken in SwiftUI right now.它现在在 SwiftUI 中被破坏了。

Further experimentation shows that the EAExternalAccessory picker seems to query the UIApplicationDelegate 's window property to find out where to display itself in.进一步的实验表明EAExternalAccessory选择器似乎在查询UIApplicationDelegatewindow属性以找出在哪里显示自己。

Alas, that being an optional property, which is not implemented in the SwiftUI's version of the AppDelegate , it only gets nil and does not show up.唉,这是一个optional属性,它没有在 SwiftUI 的AppDelegate版本中实现,它只会得到nil并且不会显示。

It also seems that the @UIApplicationDelegateAdaptor approach does not work in this case, because it does not really act as a stand-in for the real app delegate, but rather gets the methods forwarded.在这种情况下, @UIApplicationDelegateAdaptor方法似乎也不起作用,因为它并没有真正充当真正的应用程序委托的替代品,而是将方法转发。

A way to work around this is to use the UIKit application lifecycle and present your SwiftUI app via the UIHostingController .解决此问题的一种方法是使用UIKit应用程序生命周期并通过UIHostingController呈现您的 SwiftUI 应用程序。

It might also be possible to hack this via method swizzling, but if so, I don't know how.也有可能通过方法调配来破解它,但如果是这样,我不知道怎么做。

Please open a bug report.请打开错误报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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