Device Motion in WKWebView

I am loading a website with a WKWebView in my Xcode app and the website requires device motion to function. In safari, the website requests access to device motion (ios 13 feature), but in Xcode, it does not request access nor does it begin to track motion data. Using the WebView app on the app store I am able to load in the same website in a WKWebView and the motion is track meaning it is definitely possible. Any help I can get on this feature would be greatly appreciated. Thanks in advance!


Here is some more info:


ios 13 Device Motion Request JS Code:

DeviceMotionEvent.requestPermission()

.then(response => {

if (response == "granted") {

window.addEventListener("devicemotion", e => {

console.log(e.acceleration)

});

}

})

.catch(console.error);


Here is a website to demo device motion that works on the WebView app, but not in a basic WKWebView:

https://www.audero.it/demo/device-orientation-api-demo.html

Replies

Me too. No dialog appears, no error. Apple, messed up again?

I face the same problem too. The browser console prompt me that motion permission should be request by user action. (something like that). Then I append it to onclick event and the dialog finally shown in the WKWebview as above.

Currently having this same issue. Are any known solutions out yet?

I have the same issue on 03/03/2022
The solution is to set the uiDelegate on your WKWebView. The unreleased version of WKWebView (included in iOS 15 perhaps?) includes a UI delegate callback that lets you grant this permission without UI being presented. See:

https://github.com/WebKit/WebKit/blob/7891d2662792303261d10944c9ccf3b9b9d7c46b/Source/WebKit/UIProcess/API/Cocoa/WKUIDelegate.h#L154