Android WebView userAgent is set to a desktop UA instance

  • 2021-11-30 01:25:06
  • OfStack

Alipay scan code payment was used in a recent large-screen project, but webview will automatically jump to the mobile version page when loading the scan code payment link, and find out how to set it online, but no solution can be found. So I tried it myself

webview. getSettings (). setUserAgentString ("PC");

Or

webview. getSettings (). setUserAgentString ("Computer");

It can really be.

userAgent can set browser identification, Android/iphone/ipod/ipad/PC, etc. This should be similar to fuzzy search and pass similar values; It automatically loads desktop or mobile pages. The premise is that these pages should have desktop pages and mobile pages, and ua judgment is made to jump to the corresponding pages. If the transmitted ua does not recognize it, the desktop page will be automatically loaded.

Additional knowledge: Customize userAgent for webView

user-Agent user agent refers to browser, and its information includes hardware platform, system software, application software and user's personal preference. User Agent capabilities and preferences can be considered as metadata or features and descriptions of the user agent's hardware and software. By customizing user-Agent, we can read specific messages to specific browsers.


  UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectZero];
  NSString * oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
  NSLog(@"old agent :%@", oldAgent);

  //add my info to the new agent
  NSString * newAgent = [oldAgent stringByAppendingString:@" SuGrand/2.4.7 ch_appstore"];

  // or updata my info to the new agent
//  NSString * newAgent = [NSString stringWithFormat:@"Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H141"];

  NSLog(@"new agent :%@", newAgent);

  //regist the new agent
  NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
  [[NSUserDefaults standardUserDefaults] registerDefaults:dic];

In this way, the user-Agent of WebView at the time of request is the one we set. If we need to change user-Agent again during the use of WebView, we need to modify user-Agent in this way, and then re-instantiate an WebView.


  __weak typeof(self) weakSelf = self;

  [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
    __strong typeof(weakSelf) strongSelf = weakSelf;

    NSLog(@"old agent :%@", result);

    NSString *userAgent = result;
    NSString *newUserAgent = [userAgent stringByAppendingString:@" Appended Custom User Agent"];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

    strongSelf.webView = [[WKWebView alloc] initWithFrame:strongSelf.view.bounds];

    // After this point the web view will use a custom appended user agent
    [strongSelf.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
      NSLog(@"new agent :%@", result);
    }];
  }];

Related articles: