SoFunction
Updated on 2025-04-09

Android WebView userAgent is set as desktop UA instance

Alipay scan code payment was used in a large-screen project, but when the webview loads the scan code payment link, it will automatically jump to the mobile version page. I searched online for how to set it up, but no solution was found. So I tried it casually

().setUserAgentString("PC");

or

().setUserAgentString("Computer");

It's really possible.

userAgent can set the browser logo, Android/iphone/ipod/ipad/PC, etc. This should be done similar to fuzzy search, just pass similar values; it will automatically load the desktop version page or mobile version page. The premise is that these pages must have desktop and mobile pages, and the corresponding page must be redirected after UA judgment. If the transmitted ua cannot be recognized, the desktop version page will be automatically loaded.

Supplementary knowledge:Customize the userAgent of the webView

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

  UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectZero];
  NSString * oldAgent = [webView stringByEvaluatingJavaScriptFromString:@""];
  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 the WebView when requesting is what we set. If you need to change the user-Agent again during the use of the WebView, you need to modify the user-Agent in this way, and then re-instance a WebView.

  __weak typeof(self) weakSelf = self;

  [ evaluateJavaScript:@"" 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];

     = [[WKWebView alloc] initWithFrame:];

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

The above example of setting Android WebView userAgent as desktop UA is all the content I share with you. I hope you can give you a reference and I hope you support me more.