iOS - 保持与本地网络设备的网络套接字连接始终有效

iOS - Keep web-socket connection to local network device always alive

我有一台设备位于可能没有互联网连接的 Wi-Fi 本地网络中,它使用网络套接字向连接到它的设备发送通知。

但是当我锁定 phone 或按下主页按钮时,我的应用程序进入后台模式并且网络套接字连接关闭,因此从那以后任何通知都不会到达 phone.

我知道我可以通过使用远程通知(使用 Apple 推送通知服务)来做到这一点,但我的设备旨在用于任何 Wi-Fi 网络(有或没有互联网访问)或生成自己的 Wi-Fi访问点(因此无法访问 Internet)。

所以问题是:是否可以建立持久连接,例如 Android 我可以在后台服务中维护连接?

不,如果您希望您的应用程序出现在 App Store 中,则不能。
如果不是(例如,这是一个企业应用程序),您可以使用一些技巧,例如无声音频或 voip,但这会消耗大量电池电量。

In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP).
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories

unlimited backgrounding on ios with silent audio
Apple docs - background execution