iOS 13 - WkWebView: audio stops when in background

SwiftWkwebviewAvaudiosessionIos13Ios Background-Mode

Swift Problem Overview


I've an app that uses WkWebView to play audio from Soundcloud, on iOS 13 Beta 6 the audio stops when the app is not in foreground, even when Audio is on in Background Modes.

When starting the playback this assertion is thrown:

Error acquiring assertion: <NSError: 0x282cf67c0; domain: RBSAssertionErrorDomain; code: 2; reason: "Required client entitlement is missing"> {
userInfo = {
    RBSAssertionAttribute = <RBSLegacyAttribute: 0x1592432e0; requestedReason: MediaPlayback; reason: MediaPlayback; flags: PreventTaskSuspend | PreventTaskThrottleDown | WantsForegroundResourcePriority>;
} 

And after entering background state, the following assertion is thrown and the audio is suspended:

Can't end BackgroundTask: no background task exists with identifier 13 (0xd), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug. 
[ProcessSuspension] Background task expired while holding WebKit ProcessAssertion (isMainThread? 1).

This is not happening in iOS 12, where the audio plays normally when it is in background.

Swift Solutions


Solution 1 - Swift

I guess you are using Capacitor.

I'm running into the same issue as you and it looks like the last error about 'no background task exists' has something to do with Capacitor's BackgroundTask Plugin, wether you're using it or not (BackgroundTask.swift)

It's clear something changed between iOS 12 and iOS 13, but so far haven't figured out how to define the required entitlements for background audio.

EDIT: It only happens when using HTML5 Audio.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarco MartignoneView Question on Stackoverflow
Solution 1 - SwiftKim BiesbjergView Answer on Stackoverflow