【问题标题】:How can I change the UserAgent for a UIIWebView that's already been created?如何更改已创建的 UIIWebView 的 UserAgent?
【发布时间】:2014-03-14 16:42:26
【问题描述】:

更改 UserAgent 可以很好地完成:

[[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"UserAgent": @"My String" }];

如果我在调用上述代码后创建了一个新的 UIWebView,那么 UserAgent 确实会发生变化。但是,假设我想让用户更改 UserAgent、刷新页面并让新的 UserAgent 生效。上面的行似乎没有这样做。我必须创建一个新的 UIWebView 并将其 URL 设置为以前的 UIWebView 的 URL 并启动该请求。有没有更好的方法来做到这一点?

总结:

  1. 将 UserAgent 设置为默认值。
  2. 创建一个 uiwebview
  3. 转到 www.whatsmyuseragent.com 并注意 UserAgent 已设置为您想要的。
  4. 在默认值中再次将 UserAgent 设置为其他值。
  5. 在 UIWebView 中刷新页面
  6. UserAgent 不是新值。

我试着打电话

[[NSUserDefaults standardUserDefaults] 同步];

在我设置了 UserAgent 之后,它似乎并没有解决问题。

【问题讨论】:

  • 我不确定我是否完全理解您的问题,但您是否尝试过:[默认同步],使更改生效?
  • 1.将 UserAgent 设置为默认值。 2. 创建一个 uiwebview 3. 转到 www.whatsmyuseragent.com 并注意 UserAgent 设置为您想要的。 4.在默认值中再次将UserAgent设置为其他内容。 5. 刷新 UIWebView 中的页面 6. UserAgent 不是新值。我尝试调用 [[NSUserDefaults standardUserDefaults] synchronize];在我设置了 UserAgent 之后,它似乎并没有解决问题。
  • 显然另一种方法是通过一些私有 API 更新现有 UIWebView 对象的 userAgent 值。
  • hmmm 肯定在寻找一种不使用私有 API 的方法!

标签: ios objective-c cocoa-touch


【解决方案1】:

看起来就像 UIWebView 加载了一个页面。即 loadRequest 已被调用,它从默认值中读取 UserAgent 的值,然后 UIWebView 将永远设置为该 UserAgent。如果不使用私有 API,似乎没有办法在不重新创建 UIWebView 的情况下更改 UserAgent。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多