Change User Agent in UIWebView

前端 未结 14 2002
孤城傲影
孤城傲影 2020-11-22 17:03

I have a business need to be able to customize the UserAgent for an embedded UIWebView. (For instance, I\'d like the server to respond differently if, say, a user is using

14条回答
  •  天涯浪人
    2020-11-22 18:00

    To just add a custom content to the current UserAgent value, do the following:

    1 - Get the user agent value from a NEW WEBVIEW

    2 - Append the custom content to it

    3 - Save the new value in a dictionary with the key UserAgent

    4 - Save the dictionary in standardUserDefaults.

    See the exemple below:

    NSString *userAgentP1 = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *userAgentP2 = @"My_custom_value";
    NSString *userAgent = [NSString stringWithFormat:@"%@ %@", userAgentP1, userAgentP2];
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:userAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    

提交回复
热议问题