Update: Added new hack working flawlessly also on iOS 14 and newer (reflected in code, see bottom for extra details).
Since I have a solution to this nontrivial problem, I'd like to share it:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive),
name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive),
name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = []
wkWebView = WKWebView(frame: .zero, configuration: configuration)
}
@objc func willResignActive() {
disableIgnoreSilentSwitch(wkWebView)
}
@objc func didBecomeActive() {
//Always creates new js Audio object to ensure the audio session behaves correctly
forceIgnoreSilentHardwareSwitch(wkWebView, initialSetup: false)
}
And most importantly in WKNavigationDelegate
:
private func disableIgnoreSilentSwitch(_ webView: WKWebView) {
//Muting the js Audio object src is critical to restore the audio sound session to consistent state for app background/foreground cycle
let jsInject = "document.getElementById('wkwebviewAudio').muted=true;"
webView.evaluateJavaScript(jsInject, completionHandler: nil)
}
private func forceIgnoreSilentHardwareSwitch(_ webView: WKWebView, initialSetup: Bool) {
//after some trial and error this seems to be minimal silence sound that still plays
let silenceMono56kbps100msBase64Mp3 = "data:audio/mp3;base64,//tAxAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAAFAAAESAAzMzMzMzMzMzMzMzMzMzMzMzMzZmZmZmZmZmZmZmZmZmZmZmZmZmaZmZmZmZmZmZmZmZmZmZmZmZmZmczMzMzMzMzMzMzMzMzMzMzMzMzM//////////////////////////8AAAA5TEFNRTMuMTAwAZYAAAAAAAAAABQ4JAMGQgAAOAAABEhNIZS0AAAAAAD/+0DEAAPH3Yz0AAR8CPqyIEABp6AxjG/4x/XiInE4lfQDFwIIRE+uBgZoW4RL0OLMDFn6E5v+/u5ehf76bu7/6bu5+gAiIQGAABQIUJ0QolFghEn/9PhZQpcUTpXMjo0OGzRCZX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qqqqqqqg=="
//Plays 100ms silence once the web page has loaded through HTML5 Audio element (through Javascript)
//which as a side effect will switch WKWebView AudioSession to AVAudioSessionCategoryPlayback
var jsInject: String
if initialSetup {
jsInject =
"var s=new Audio('\(silenceMono56kbps100msBase64Mp3)');" +
"s.id='wkwebviewAudio';" +
"s.play();" +
"s.loop=true;" +
"document.body.appendChild(s);"
} else {
//Restore sound hack
jsInject = "document.getElementById('wkwebviewAudio').muted=false;"
}
webView.evaluateJavaScript(jsInject, completionHandler: nil)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//As a result the WKWebView ignores the silent switch
forceIgnoreSilentHardwareSwitch(webView, initialSetup: true)
}
Interestingly a related Safari problem is mentioned here: IOS WebAudio only works on headphones where @Spencer Evans workaround looks very similar to mine.
However when I tried to apply his shorter base64 silence sound it didn't work for WKWebView
, so I'm providing my own minimal silence sound tested on iOS12.
Why it works?
Playing an <audio>
or <video>
element (which in the workaround happens to be non audible silence) changes WKWebView
audio session category from AVAudioSessionCategoryAmbient
to AVAudioSessionCategoryPlayback
. This will be valid until next load request resets it.
It's all great till the app is backgrounded. But upon subsequent foregrounding things will break in 2 possible ways:
- user needs to tap for the sounds to reappear
- rarely no user input will help and the WKWebView lands in semi frozen state
To counter that^ the hack is reverted with disableIgnoreSilentSwitch(wkWebView)
and later reenabled with forceIgnoreSilentHardwareSwitch(wkWebView, initialSetup: false)
Since WKWebView
core runs in an external process it cannot be accessed the way UIWebView
shared (with our app) AVAudioSession
can be.
Verified for:
iOS 11.4
iOS 12.4.1
iOS 13.3
iOS 14.1
iOS 14.5.1
iOS 14.8
iOS 15.0
iOS 16.0
iOS 17.0
iOS 14 update
Situation got pretty bad in iOS 14 where obsolete audio tag .src=null
trick stopped working. Technically .src=null
does work for a very short window of time (one can revert the hack using .src
during initial setup). However once the silence loop is playing it becomes useless.
The new trick relies on .mute
which miraculously works across all iOS versions including iOS14 (but only when accessing documentById directly not a var). No mediacenter when locking the screen neither. It took a lot of research, but we got it.