如何保持GCKCastSession在应用程序转到后台时保持活动状态
我正在使用适用于iOS的最新Google Cast SDK(3.1.1.10003版),我们的应用程序正在远程控制Google Cast设备,例如,它会更改其音量。 它也需要这样做,当我们的应用程序去背景。
但是, GCKSessionManager
调用suspendSessionWithReason:
当应用程序转到后台。 这表示,会话将被暂停,因此我们的应用程序不能再控制它。
如何让GCKSessionManager
在应用程序转到后台时不暂停会话?
编辑 :我在下面提供了一个解决scheme,但由于重新连接时显着的延迟,这不是用户友好的。
由于Google Cast SDK在应用程序转到后台时调用GCKSessionManager.suspendSessionWithReason
,因此一种解决scheme是使用不同的实现replace此方法,以检查原因是.AppBackgrounded
,然后实际上忽略了调用。
然后,SDK不会在应用程序转到后台时立即终止会话,但会在稍后暂停会话。 然而,会话可以从后台模式重新启动 – 但是这需要花费大量时间,并且从用户的angular度来看延迟是负面的。 此外,会议在几秒钟后不再“与之通话”,会再次中止。
任何更好的解决scheme仍然非常感激。
extension GCKSessionManager { static func ignoreAppBackgroundModeChange() { let oldMethod = class_getInstanceMethod(GCKSessionManager.self, #selector(GCKSessionManager.suspendSessionWithReason)) let newMethod = class_getInstanceMethod(GCKSessionManager.self, #selector(GCKSessionManager.suspendSessionWithReasonIgnoringAppBackgrounded)) method_exchangeImplementations(oldMethod, newMethod) } func suspendSessionWithReasonIgnoringAppBackgrounded(reason: GCKConnectionSuspendReason) -> Bool { guard reason != .AppBackgrounded else { return false } return suspendSessionWithReason(reason) } }
我们现在要做的就是调用GCKSessionManager.ignoreAppBackgroundModeChange()
。
编辑:
从最新的Google Cast SDK开始,它有一个新的选项可以使会话保持在后台。