setting ios UserAgent via NSUserDefaults: works half the time?

£可爱£侵袭症+ 提交于 2020-01-04 02:42:08

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/13247305/setting-ios-useragent-via-nsuserdefaults-works-half-the-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!