网页是否可能检测到已从Webview应用程序访问了该网页?

时间:2018-09-14 02:27:55

标签: javascript android ios react-native webview

假设我有一个仅包含2个页面的网站。

  1. http://example.com
  2. http://example.com/webview.html

然后我创建了一个webview react本机应用程序,它可能很简单:

App.js

import React, { Component } from 'react';
import { WebView } from 'react-native';

export default class MyWeb extends Component {
  render() {
    return (
      <WebView
        source={{uri: 'http://example.com/webview.html'}}
        style={{marginTop: 20}}
      />
    );
  }
}

现在我的问题是:该网页是否有可能检测到它是从we​​bview应用程序访问的?

假设我在webview.html文件中有一个条件,例如:

if ( webview ) {
  return 'webview';
}
else {
  return 'normal browser'
}

也许像插入javascript一样,也许是从Webview到网页的变量?然后,我可以使用该变量检查其存在,然后检查是否存在,然后是webview,否则不存在。

我知道javascript可以检测使用的浏览器,屏幕分辨率,操作系统等。这有可能吗?

该应用具有Android和iOS的目标操作系统。

1 个答案:

答案 0 :(得分:1)

尝试使用navigator.userAgent。

您可以在目标应用程序的Web视图中设置自定义UserAgent。

对于iOS:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *newUserAgent = [userAgent stringByAppendingString:@" Your Custom Key"];//your custom key
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}

对于Android:

WebSettings settings = webview.getSettings();

String ua = webview.getSettings().getUserAgentString();  
webview.getSettings().setUserAgentString(ua+"; Your Custom Key");

在h5中,您可以获取ua并确定目标

相关问题