detect ipad/iphone webview via javascript

2019-01-01 03:24发布

Is there a way to differ via javascript if the website runs inside the ipad safari or inside an application WebView?

13条回答
牵手、夕阳
2楼-- · 2019-01-01 03:32

@ Sod, Well i don’t have answer, but i am not convinced why you want to check, Since, browser engine whether its safari ( Browser ) or Application will be same its Webkit only, Yes Application can configure the Browser engine capabilities like, whether application wants to run JS or Display Image etc…

I believe, you must check for certain property whether Flash supported by Browser or whether browser displays image or not, or probably may be you would like to check the screen size,

查看更多
姐姐魅力值爆表
3楼-- · 2019-01-01 03:34

I've tried all these solutions but didn't work in my case,
I was going to detect Telegram inside Webview. I've noticed Safari is changing phone style text to a link with "tel:" prefix, so I've used this to write this code, you can test it : jsfiddle

<!DOCTYPE html>
<html>
<head></head>
<body>
<ul id="phone" style="opacity:0">
    <li>111-111-1111</li>
</ul>
</body>
</html>

<script>

    var html = document.getElementById("phone").innerHTML;

    if (navigator.platform.substr(0,2) === 'iP') {

        if (html.indexOf('tel:') == -1)
            alert('not safari browser');
        else
            alert('safari browser');
    }
    else
        alert('not iOS');
</script>
查看更多
其实,你不懂
4楼-- · 2019-01-01 03:37

I don't think there's anything specific you can use in client-side Javascript, but if you have control over what the originating UIWebView can do, you might want to consider playing with the user agent string it generates, and testing for that in your client-side Javascript instead? A bit of a hack I know, but hey… This question may give some pointers on tweaking the user agent:

Change User Agent in UIWebView (iPhone SDK)

查看更多
人气声优
5楼-- · 2019-01-01 03:39

I know this code will check if it is being accessed from an icon added to the home screen:

if (window.navigator.standalone == true) {
//not in safari
}

but I'm not sure how it would react in a UIWebView. The only other solution I could think of is getting the user agent or using - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType and replacing the query string of the page you are accessing with something the page uses to identify that it is being accessed from a web view.

查看更多
残风、尘缘若梦
6楼-- · 2019-01-01 03:40

Would suggest using Modernizr, and checking for indexeddb like this. You could cross-check that with user agent configuration (device, OS, browser, etc), but pure feature detection seems more recommended.

查看更多
骚的不知所云
7楼-- · 2019-01-01 03:40

I have found a simple solution to detect iPhone or iPad. This works for me fine.

var is_iPad = navigator.userAgent.match(/iPad/i) != null;
var is_iPhone = navigator.userAgent.match(/iPhone/i) != null;
    if(is_iPad || is_iPhone == true){
        //perform your action
    }
查看更多
登录 后发表回答