PDA

View Full Version : Change useragent




themacster298
Jul 19, 2012, 03:33 PM
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"]];



chown33
Jul 19, 2012, 04:37 PM
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"

themacster298
Jul 20, 2012, 08:33 PM
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"


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

gnasher729
Jul 21, 2012, 11:41 AM
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"]];

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