Set user agent with WebView with react-native

后端 未结 9 1530
闹比i
闹比i 2021-02-19 13:26

I want to modify the user agent string in a WebView so that on the server side I can detect that the request has come from my react-native app. I want to do this using the sourc

9条回答
  •  野性不改
    2021-02-19 13:47

    You just can set it as a prop in your WebView.

    I'm doing the following:

    on iOS (I set the userAgent in AppDelegate.m)

    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];
    

    on Android (I set the userAgent in JS in my WebView):

    
    

    now I'm always having a userAgent like "DeviceInfo - MYAPPNAME - Platform". We're doing the same like you and it works how it's supposed to do.

提交回复
热议问题