【问题标题】:iOS simulator getting connectionDidReceiveResponse with no InternetiOS模拟器在没有互联网的情况下获得连接DidReceiveResponse
【发布时间】:2012-01-17 19:23:06
【问题描述】:

我一直在考虑是否应该发布这个问题,但我试图了解可能发生的事情并且不知道。

我的应用程序从服务器下载一些 xml 文件。 我在没有互联网连接的情况下尝试过(我的路由器已关闭,wifi 已关闭)但模拟器不断收到 200 http 响应并显示下载的数据。

我已经记录了响应的状态码并得到 200,并且 currentRequest url 和信息是正确的(资源的 url)。

模拟器获取数据的地方有缓存吗?

模拟器状态栏上的网络图标常亮。

【问题讨论】:

    标签: cocoa-touch ios-simulator nsurlconnection


    【解决方案1】:

    解决这个问题的最简单方法是使用NSMUtableURLRequest的以下方法:

    [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    

    NSURLRequestReloadIgnoringLocalAndRemoteCacheData 值是最严格的“永远不要缓存任何东西!”选项,但如果您想要更具体的控制,请查看 NSURLRequestCachePolicy 枚举的文档以了解所有可能的选项。

    【讨论】:

      【解决方案2】:

      你可以试试这个代码。它不缓存数据。

       NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
      [headers setValue:@"no-cache" forKey:@"Cache-Control"];
      [headers setValue:@"no-cache" forKey:@"Pragma"];
      
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:TIMEOUT_REQUEST];
      [request setHTTPMethod:@"GET"];
      [request setAllHTTPHeaderFields:headers];
      

      【讨论】:

        猜你喜欢
        • 2021-07-21
        • 1970-01-01
        • 2016-01-20
        • 2016-01-09
        • 2011-06-21
        • 1970-01-01
        • 2017-09-09
        • 2015-05-24
        • 1970-01-01
        相关资源
        最近更新 更多