Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting ios UserAgent via NSUserDefaults: works half the time?

This is driving me crazy. I have two apps that are just UIWebViews, using XCode 4.5.2 and the iOS 6.0 SDK. I want to change the UserAgent of the web calls. I'm using this code in both:

NSString *secretagent = @"MyUserAgent";
NSDictionary *dictionary = [[NSDictionary alloc]
    initWithObjectsAndKeys:secretAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

What's killing me is that it works in one and not in the other, based on my scanning of the web logs. The one that doesn't work is still getting the UserAgent key changed, based on my checking with objectsForKey:@"UserAgent". It's just that nothing actually changes on the web request.

Any ideas? It's 3:30 AM and I just can't figure this out...

like image 357
Joseph Cheek Avatar asked Jan 20 '26 02:01

Joseph Cheek


1 Answers

figured it out. you can't alter the user agent after the webview has been initialized. i moved the code to viewDidLoad, before I initialize the webview, and it works.

like image 161
Joseph Cheek Avatar answered Jan 21 '26 17:01

Joseph Cheek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!