通过 NSUserDefaults 设置 ios UserAgent:有一半的时间? [英] setting ios UserAgent via NSUserDefaults: works half the time?

查看:22
本文介绍了通过 NSUserDefaults 设置 ios UserAgent:有一半的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯.我有两个应用程序只是 UIWebViews,使用 XCode 4.5.2 和 iOS 6.0 SDK.我想更改网络调用的 UserAgent.我在两者中都使用此代码:

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

让我失望的是,根据我对网络日志的扫描,它在一种情况下有效,而在另一种情况下无效.根据我对objectsForKey:@UserAgent"的检查,不起作用的仍然是更改了UserAgent密钥.只是网络请求实际上没有任何变化.

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.

有什么想法吗?现在是凌晨 3 点 30 分,我就是想不通...

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

推荐答案

想通了.初始化 webview 后,您无法更改用户代理.在初始化 webview 之前,我将代码移动到 viewDidLoad,并且它可以工作.

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.

这篇关于通过 NSUserDefaults 设置 ios UserAgent:有一半的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆