我想在WebView中修改用户代理字符串,以便在服务器端可以检测到该请求来自我的本机应用程序。我想使用WebView中的源道具来做到这一点。
如何在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}} />
现在我总是有一个userAgent,例如“ DeviceInfo-MYAPPNAME-Platform”。我们和您一样在做同样的事情,它应该按照预期的方式工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句