Change User Agent in UIWebView

前端 未结 14 1823
孤城傲影
孤城傲影 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 17:41

    I faced the same question. I want to add some info to the user-agent, also need to keep the original user-agent of webview. I solved it by using the code below:

        //get the original user-agent of webview
    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:@" Jiecao/2.4.7 ch_appstore"];
    NSLog(@"new agent :%@", newAgent);
    
    //regist the new agent
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    

    Use it before you instancing webview.

    0 讨论(0)
  • 2020-11-22 17:49

    Modern Swift

    Here's a suggestion for Swift 3+ projects from StackOverflow users PassKit and Kheldar:

    UserDefaults.standard.register(defaults: ["UserAgent" : "Custom Agent"])
    

    Source: https://stackoverflow.com/a/27330998/128579

    Earlier Objective-C Answer

    With iOS 5 changes, I recommend the following approach, originally from this StackOverflow question: UIWebView iOS5 changing user-agent as pointed out in an answer below. In comments on that page, it appears to work in 4.3 and earlier also.

    Change the "UserAgent" default value by running this code once when your app starts:

    NSDictionary *dictionary = @{@"UserAgent": @"Your user agent"};
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    See previous edits on this post if you need methods that work in versions of iOS before 4.3/5.0. Note that because of the extensive edits, the following comments / other answers on this page may not make sense. This is a four year old question, after all. ;-)

    0 讨论(0)
  • 2020-11-22 17:49

    The only problem I have found was change user agent only

    - (BOOL)application:(UIApplication *)application
            didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        NSDictionary *dictionary = [NSDictionary 
            dictionaryWithObjectsAndKeys:
            @"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", 
            @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    }
    
    0 讨论(0)
  • 2020-11-22 17:50

    It should work with an NSMutableURLRequest as Kuso has written.

    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.google.com/"]];
    [urlRequest setValue: @"iPhone" forHTTPHeaderField: @"User-Agent"]; // Or any other User-Agent value.
    

    You'll have to use NSURLConnection to get the responseData. Set the responseData to your UIWebView and the webView should render:

    [webView loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL];
    
    0 讨论(0)
  • 2020-11-22 17:51

    Using @"User_Agent" simply causes a custom header to appear in the GET request.

    User_agent: Foobar/1.0\r\n
    User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Mobile/7D11\r\n

    The above is what appears in the dissected HTTP packet, essentially confirming what Sfjava was quoting from that forum. It's interesting to note that "User-Agent" gets turned into "User_agent."

    0 讨论(0)
  • Apple will soon stop accepting apps with UIWebView. Find below for how you could change the user agent in WKWebView.

    let config = WKWebViewConfiguration()
    
    config.applicationNameForUserAgent = "My iOS app"
    
    webView = WKWebView(frame: <the frame you need>, configuration: config)
    
    0 讨论(0)
提交回复
热议问题