I am creating an iOS app that primarily makes use of a WKWebView to show net content material which incorporates HTML5 components for enjoying radio streams. I would like these streams to proceed taking part in when the app goes into the background, and I wish to management playback (Play/Pause, Subsequent/Earlier) by way of the usual iOS media controls (lock display screen, Management Heart) utilizing MPNowPlayingInfoCenter and MPRemoteCommandCenter.
The Drawback:
Whereas playback works when the app is within the foreground, I am encountering crashes or audio stoppage when the app goes into the background or shortly after interacting with the native media controls. The particular error I persistently see within the console logs is:
Error buying assertion:
... - ProcessAssertion::acquireSync Failed to amass RBS assertion 'WebKit Media Playback' for course of with PID=..., error: Error Area=RBSServiceErrorDomain Code=1 ...
This occurs regardless that I’ve enabled the required background mode…
Enabled “Audio, AirPlay, and Image in Image” below Background Modes in Signing & Capabilities
What I’ve Carried out:
- AVAudioSession: Configured in AppDelegate’s didFinishLaunchingWithOptions methodology.
- WKWebView Configuration: In my WebViewController’s viewDidLoad methodology.
- JavaScript Bridge (WKScriptMessageHandler): Carried out userContentController(_:didReceive:) in WebViewController to obtain messages like “mediaPlayed”, “mediaPaused”, “updateMetadata” from JavaScript. This handler calls strategies on a singleton AudioManager.
- AudioManager Singleton: This class manages MPNowPlayingInfoCenter.default() and MPRemoteCommandCenter.shared().
- It units metadata acquired from JS.
- It registers handlers for playCommand, pauseCommand, nextTrackCommand, previousTrackCommand.
- These command handlers publish a Notification (.executeJavaScript) which the WebViewController observes.
- The WebViewController’s observer (executeJsFromNotification) calls webView.evaluateJavaScript(…) to run capabilities like playCurrentAudio(), pauseCurrentAudio(), skipToNextAudio(), skipToPreviousAudio() within the WebView’s JS context.
- AudioManager calls AVAudioSession.sharedInstance().setActive(true) in handleMediaPlayed and setActive(false) in handleMediaStopped.
What Occurs:
The crash appears linked to the WKWebView’s course of not having the required entitlement to keep up playback assertions whereas the app will not be totally within the foreground.
My Questions:
- Why is the WKWebView course of apparently requiring the com.apple.runningboard.assertions.webkit entitlement for background audio playback when the usual “Audio” background mode ought to suffice?
- Is that this a identified bug or limitation in sure iOS/WebKit variations? (I am testing on iOS 17.6)