Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

themacster298

macrumors member
Original poster
Feb 20, 2011
55
0
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:

Code:
[webView setCustomUserAgent:[NSString stringWithFormat:@"Mobile/8A293 Safari/6531.22.7"]];
 
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:
Code:
[NSString stringWithFormat:@"Mobile/8A293 Safari/6531.22.7"]
can be simplified to:
Code:
@"Mobile/8A293 Safari/6531.22.7"
 
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:
Code:
[NSString stringWithFormat:@"Mobile/8A293 Safari/6531.22.7"]
can be simplified to:
Code:
@"Mobile/8A293 Safari/6531.22.7"

Thanks for the tips. I finally figured it out with Apples documentation code.

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

Code:
[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"]];
 
Thanks for the tips. I finally figured it out with Apples documentation code.

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

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

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

Code:
[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"];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.