开发者

Add window.navigator.standalone to UIWebView DOM

开发者 https://www.devze.com 2023-01-12 12:57 出处:网络
You know how a \"webapp\" can be installed via Mobile Safari by tapping the + button? And by launching this app via the homescreen your webapp knows that it was launched from the homescreen because wi

You know how a "webapp" can be installed via Mobile Safari by tapping the + button? And by launching this app via the homescreen your webapp knows that it was launched from the homescreen because window.navigator.standalone is set to 1...

Well..

I made an iOS app with a UIWebView that loads a web app. And now I'd like to add the window.navigat开发者_如何转开发or.standalone property to the dom of my webapp... is that possible?

Thanks


Inject some Javascript into the webview from the objective C to set the parameter manually

[webView stringByEvaluatingJavaScriptFromString:@"window.navigator.standalone=1"];

-Dx


It's not possible. navigator.standalone is read-only like most native navigator properties. I just checked with an iPhone Emulator in Inspector.


Inject the following into your webview

let script= "Object.defineProperty(navigator, 'standalone', {get:function(){return true;}});"
evaluateJavaScript(script, completionHandler: nil)

That way you can even override read only objects

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号