[英]Javascript is not working in React-Native WebView for Android (javaScriptEnabled = true is set)
[英]Set user agent with WebView with react-native
我想修改 WebView 中的用戶代理字符串,以便在服務器端我可以檢測到請求來自我的 react-native 應用程序。 我想使用 WebView 中的 source prop 來做到這一點。
我如何為 IOS 和 Android 執行此操作?
您只需將其設置為 WebView 中的道具即可。
我正在做以下事情:
在 iOS 上(我在 AppDelegate.m 中設置了 userAgent)
NSString *deviceType = [UIDevice currentDevice].model;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *newAgent = [oldAgent stringByAppendingString:@" MYAPPNAME - iOS - "];
newAgent = [newAgent stringByAppendingString:deviceType];
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
在 Android 上(我在 WebView 的 JS 中設置了 userAgent):
<WebView
userAgent={DeviceInfo.getUserAgent() + " - MYAPPNAME - android "}
ref={"WEBVIEW"}
automaticallyAdjustContentInsets={false}
source={{uri: this.state.url}} />
現在我總是有一個像“DeviceInfo - MYAPPNAME - Platform”這樣的用戶代理。 我們正在做和你一樣的事情,它應該如何工作。
對於安卓,你只需要設置userAgent
屬性,例如:
<WebView
source={{ uri: "https://stackoverflow.com" }}
userAgent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
/>
對於 iOS,您需要在AppDelegate.m
實現中的didFinishLaunchingWithOptions
方法(在行return YES;
)中添加以下代碼:
NSString *userAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
該庫現在提供了一種獲取 ios 和 android 用戶代理的方法:
https://github.com/react-native-community/react-native-device-info#getuseragent
this.state = {
userAgent: "",
}
componentDidMount = async () => {
this.setState({ userAgent: await DeviceInfo.getUserAgent() })
}
<WebView
source={{ uri: "https://stackoverflow.com" }}
userAgent={this.state.userAgent}
>
適用於 android 和 ios 的 windows 手機用戶代理
安卓:
mWebView.getSettings().setUserAgentString("Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)");
IOS:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:@"Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)" forkey:@"UserAgent"]];
只需將此添加到用戶代理,您就可以在將來更新版本,我使用的是因為谷歌登錄給了我問題
userAgent="Mozilla/5.0 (Linux; Android 10; Android SDK built for x86 Build/LMY48X) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.117 Mobile Safari/608.2.11"
這不是答案,而是一種解決方法。
我意識到我想要做的是確定對我的 ruby on rails 服務器的請求是來自 mobile safari 還是來自我的 react-native 應用程序的 webView。 為了解決這個問題,我剛剛安裝了browser
gem,然后做了
browser = Browser.new(request.user_agent)
from_app = browser.platform.ios_webview?
如果有人可以對我最初的問題給出明確的答案,我會選擇那個答案而不是我的答案。
對於 iOS,您可以使用 WKWebView 而不是支持 userAgent 設置的 WebView。 https://github.com/react-native-community/react-native-webview/blob/master/docs/Reference.md#useragent
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
這是一個可怕的想法。 只需使用自定義標題:
X-MY-CUSTOM-HEADER = MyAppName.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.