Change useragent

Discussion in 'Mac Programming' started by themacster298, Jul 19, 2012.

  1. macrumors member

    Feb 20, 2011
    How would I change the user agent of a webview in Xcode?

    I am trying to change the user agent to the ios safari.

    I have tried this and putting into the viewdidload method:

    [webView setCustomUserAgent:[NSString stringWithFormat:@"Mobile/8A293 Safari/6531.22.7"]];
  2. macrumors 604

    Aug 9, 2009
    And what happened?

    It didn't work? It worked but the result wasn't what you expected?

    If it didn't work, describe how you determined this. Did you see the user-agent string on the server side? Something else?

    What have you done to debug this? Have you checked that webView is non-nil at that point? Have you checked that no other value is assigned to webView after that point? Have you NSLogged the value of customUserAgent when you start an actual request?

    Rules of Thumb:
    1. Be specific.
    2. Post your code.
    3. Describe what you expected to happen.
    4. Describe what actually happened.

    And always be prepared to answer the question "What have you tried?".
    When doing so, follow the 4 rules of thumb.

    BTW, this code:
    [NSString stringWithFormat:@"Mobile/8A293 Safari/6531.22.7"]
    can be simplified to:
    @"Mobile/8A293 Safari/6531.22.7"
  3. thread starter macrumors member

    Feb 20, 2011
    Thanks for the tips. I finally figured it out with Apples documentation code.

    For any Googlers or curious. this is the code used:

    [webView setCustomUserAgent:[NSString stringWithFormat:@"Mozilla/5.0 (iPod; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"]];
  4. macrumors G5


    Nov 25, 2005
    Your use of stringWithFormat is totally unnecessary, inefficient, and dangerous. stringWithString would be unneceesary and inefficient, but at least not dangerous. Just use

    [webView setCustomUserAgent:@"Mozilla/5.0 (iPod; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"];

Share This Page