简体   繁体   English

iOS UIWebView通过附加字符串设置UserAgent无效

[英]iOS UIWebView set UserAgent by Append String not work

Here's my code: 这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    NSString *tagString = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69/YoyaOrg-iOS";
//    NSString *tag = @"YoyaOrg-iOS";
NSString *defaultUserAgent = [self.mainWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *newUserAgent = [NSString stringWithFormat:@"%@ YoyaOrg-iOS",defaultUserAgent];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
self.mainWebView.scrollView.bounces = NO;
[self.mainWebView loadRequest:request];
}
 NSLog(@"webUserAgent:%@",defaultUserAgent);

After append string "YoyaOrg-iOS",but the log output still does not contain "YoyaOrg-iOS".if i replace UserAgent as: 在附加字符串“ YoyaOrg-iOS”之后,但日志输出仍不包含“ YoyaOrg-iOS”。如果我将UserAgent替换为:

 NSString *tagString = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69/YoyaOrg-iOS";
 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:tagString, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

then the userAgent i get is what i want;I can't make it and had wasted much time; 然后我得到的userAgent是我想要的;我做不到,浪费了很多时间; TKS for any answers. TKS的任何答案。 TKS for any help! TKS寻求任何帮助!

Just for putting the postfix to your user-agent do the following in the didFinishLaunchingWithOptions function in your AppDelegate.swift 仅为了将后缀放入用户代理,请在didFinishLaunchingWithOptions中的didFinishLaunchingWithOptions函数中执行以下操作

    /* changing User Agent system-wide */
    UserDefaults.standard.register(
        defaults: [
            "UserAgent" : "\(UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent;")) YOUR_POSTFIX_TO_USER_AGENT"
        ]
    )

For brand new user agent you should get rid from UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent;") call. 对于全新的用户代理,您应该摆脱UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent;")调用。

I am also looking for a way to change user agent. 我也在寻找一种更改用户代理的方法。 I learnt that I should change user agent when instantiating the webview using code like [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}]; 我了解到,在使用[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];类的代码实例化[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];时,应该更改用户代理[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}]; and print the user Agent in the method webViewDidFinishLoad: using code NSLog(@"UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]); 并使用代码NSLog(@"UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]);webViewDidFinishLoad:方法中打印用户代理NSLog(@"UserAgent = %@", [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]); to see if the user agent is changed or not. 查看用户代理是否已更改。

then I found if I put the code [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"] before the webview loads URL request, like this 然后发现是否在代码加载URL请求之前将代码[webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]

     NSString *userAgent = [_webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *mystring = @" mystring";
    NSString *customUserAgent = [userAgent stringByAppendingString:mystring];

    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];

the result is not only the user agent is unchanged but also the OC-JS interaction codes I wrote using JavaScriptCore is disabled! 结果不仅是用户代理未更改,而且我使用JavaScriptCore编写的OC-JS交互代码也被禁用!

I found a way to fix this: init a second UIWebView which does nothing, only to get the default user agent. 我找到了解决此问题的方法:初始化第二个UIWebView,它什么都不做,只是为了获取默认的用户代理。 here's my final solution to append the default user agent string: 这是我追加默认用户代理字符串的最终解决方案:

    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _webView.scalesPageToFit = YES;

    //init another webView to get the default user agent
    NSString *userAgent = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *mystring = @" mystring";
    NSString *customUserAgent = [userAgent stringByAppendingString:mystring];

    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":customUserAgent}];

this works smoothly :) 这工作顺利:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM