iPadOS:通过 NEHotspotConfiguration 连接的网络在一段时间后断开连接

iPadOS: Network connected via NEHotspotConfiguration disconnects after a while

我的应用使用 NEHotspotConfigurationManager 通过 Wi-Fi 将自身连接到特定设备。该设备充当 WPA2 接入点。在较旧的 iOS 版本(iOS 12 及更低版本)中,一切正常,但在 iPadOS/iOS 13 中,设备每次都会在一段时间后断开连接。如何在不永久存储 NEHotspotConfiguration 的情况下保持连接?

我怀疑它与一项新功能有关 - Multiple Windows(我的应用程序不支持)。原因是在我的 NEHotspotConfiguration 中,我将 joinOnce 标志设置为 true(因为设备的网络不应该在应用程序之外使用)。 Apple's documentation 状态:

When joinOnce is set to true, the hotspot remains configured and connected only as long as the app that configured it is running in the foreground. The hotspot is disconnected and its configuration is removed when any of the following events occurs:

  • The app stays in the background for more than 15 seconds.
  • The device sleeps.
  • The app crashes, quits, or is uninstalled.
  • The app connects the device to a different Wi-Fi network.

可能我的应用被误认为离开了前台。

joinOnce 设置为 false 使应用程序保持连接状态,但这不是一个可接受的解决方案,因为我的设备不提供互联网连接并且不能在外部使用应用

以下是我如何应用热点配置:

let hotspotConfiguration = NEHotspotConfiguration(ssid: self.ssid, passphrase: self.passphrase, isWEP: false)
hotspotConfiguration.joinOnce = true
       
NEHotspotConfigurationManager.shared.apply(hotspotConfiguration) { error in
    // connection is successfully applied
    // and about 15 seconds later it is lost.
}

我希望在 joinOnce 标志设置为 true 时保持连接。

Apple 似乎已经在 iOS 13.4 beta 2 中解决了这个问题,它现在可以与 joinOnce = true 一起使用,如文档所述。 Related discussion on Apple's forum