• <i id='xtniW'><tr id='xtniW'><dt id='xtniW'><q id='xtniW'><span id='xtniW'><b id='xtniW'><form id='xtniW'><ins id='xtniW'></ins><ul id='xtniW'></ul><sub id='xtniW'></sub></form><legend id='xtniW'></legend><bdo id='xtniW'><pre id='xtniW'><center id='xtniW'></center></pre></bdo></b><th id='xtniW'></th></span></q></dt></tr></i><div id='xtniW'><tfoot id='xtniW'></tfoot><dl id='xtniW'><fieldset id='xtniW'></fieldset></dl></div>

    <small id='xtniW'></small><noframes id='xtniW'>

      <tfoot id='xtniW'></tfoot>
          <bdo id='xtniW'></bdo><ul id='xtniW'></ul>

      1. <legend id='xtniW'><style id='xtniW'><dir id='xtniW'><q id='xtniW'></q></dir></style></legend>
      2. UIWebView iOS5 改变用户代理

        UIWebView iOS5 changing user-agent(UIWebView iOS5 改变用户代理)

          • <small id='XmDnh'></small><noframes id='XmDnh'>

            <legend id='XmDnh'><style id='XmDnh'><dir id='XmDnh'><q id='XmDnh'></q></dir></style></legend>

            <i id='XmDnh'><tr id='XmDnh'><dt id='XmDnh'><q id='XmDnh'><span id='XmDnh'><b id='XmDnh'><form id='XmDnh'><ins id='XmDnh'></ins><ul id='XmDnh'></ul><sub id='XmDnh'></sub></form><legend id='XmDnh'></legend><bdo id='XmDnh'><pre id='XmDnh'><center id='XmDnh'></center></pre></bdo></b><th id='XmDnh'></th></span></q></dt></tr></i><div id='XmDnh'><tfoot id='XmDnh'></tfoot><dl id='XmDnh'><fieldset id='XmDnh'></fieldset></dl></div>

              <tfoot id='XmDnh'></tfoot>
                  <tbody id='XmDnh'></tbody>
                • <bdo id='XmDnh'></bdo><ul id='XmDnh'></ul>
                • 本文介绍了UIWebView iOS5 改变用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在 iOS 5 中更改 UIWebView 的用户代理?

                  How can I change the user-agent of UIWebView in iOS 5?

                  到目前为止我做了什么:使用委托回调,拦截 NSURLRequest,创建一个新的 url 请求并将它的用户代理设置为我想要的任何内容,然后下载数据并使用loadData:MIMEType:...."重新加载 UIWebView.

                  What I have done so far: Using the delegate call back, intercept the NSURLRequest, create a new url request and set it's user-agent as whatever I want, then download the data and reload the UIWebView with "loadData:MIMEType:....".

                  问题:这会导致无限递归,我在其中加载数据,这会回调委托,实习生会调用委托....

                  Problem: This causes infinite recursion, where I load the data, which calls the delegate back, which intern calls the delegate....

                  这是委托方法:

                  - (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
                  
                  
                  
                      dispatch_async(kBgQueue, ^{
                          NSURLResponse *response = nil;
                          NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[request URL]];
                          NSDictionary *headers = [NSDictionary dictionaryWithObject:
                                                   @"custom_test_agent" forKey:@"User-Agent"];
                          [newRequest setAllHTTPHeaderFields:headers];
                          [self setCurrentReqest:newRequest];
                          NSData *data = [NSURLConnection sendSynchronousRequest:newRequest 
                                                               returningResponse:&response 
                                                                           error:nil];
                          dispatch_sync(dispatch_get_main_queue(), ^{
                              [webView loadData:data 
                                       MIMEType:[response MIMEType] 
                               textEncodingName:[response textEncodingName] 
                                        baseURL:[request URL]];
                          });
                      });
                  
                      return YES;
                  }
                  

                  推荐答案

                  通过在应用启动时运行一次此代码来更改UserAgent"默认值:

                  Change the "UserAgent" default value by running this code once when your app starts:

                  NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Your user agent", @"UserAgent", nil];
                  [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];  
                  

                  我使用它取得了巨大的成功,但想添加更多细节.要获取用户代理,您可以启用开发人员"菜单,设置用户代理,然后连接到此站点以将其打印出来:WhatsMyAgent.同样,您可以使用任何类型的移动设备进行连接,也可以通过这种方式进行连接.顺便说一句,这在 iOS7+ 中仍然可以正常工作

                  I have used this with great success, but want to add additional details. To get a user agent, you can enable the "Developer" menu, set the user agent, and then connect to this site to get it printed out for you: WhatsMyAgent. Likewise you can connect using any kind of mobile device, and get it that way too. BTW this is still working just fine in iOS7+

                  这篇关于UIWebView iOS5 改变用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  EAAccessory/EASession teardown during background(EAAccessory/EASession 在后台拆除)
                  Getting an NSArray of a single attribute from an NSArray(从 NSArray 获取单个属性的 NSArray)
                  ImageIO: lt;ERRORgt; JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?(ImageIO:lt;错误gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?)
                  How to get indexPath.row of tableView which is currently being displayed?(如何获取当前正在显示的 tableView 的 indexPath.row?)
                  Xcode iOS organizer submit to app store yields quot;The archive is invalidquot; error(Xcode iOS 管理器提交到应用商店产生“存档无效;错误)
                  MFMessageComposeViewController alloc returns nil(MFMessageComposeViewController alloc 返回 nil)

                  <tfoot id='3LL5K'></tfoot>

                  <small id='3LL5K'></small><noframes id='3LL5K'>

                  <legend id='3LL5K'><style id='3LL5K'><dir id='3LL5K'><q id='3LL5K'></q></dir></style></legend>

                      <bdo id='3LL5K'></bdo><ul id='3LL5K'></ul>
                          <tbody id='3LL5K'></tbody>

                      • <i id='3LL5K'><tr id='3LL5K'><dt id='3LL5K'><q id='3LL5K'><span id='3LL5K'><b id='3LL5K'><form id='3LL5K'><ins id='3LL5K'></ins><ul id='3LL5K'></ul><sub id='3LL5K'></sub></form><legend id='3LL5K'></legend><bdo id='3LL5K'><pre id='3LL5K'><center id='3LL5K'></center></pre></bdo></b><th id='3LL5K'></th></span></q></dt></tr></i><div id='3LL5K'><tfoot id='3LL5K'></tfoot><dl id='3LL5K'><fieldset id='3LL5K'></fieldset></dl></div>