Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIWebView request.response=nil问题_Ios_Swift_Webview_Nsurlcache - Fatal编程技术网

Ios UIWebView request.response=nil问题

Ios UIWebView request.response=nil问题,ios,swift,webview,nsurlcache,Ios,Swift,Webview,Nsurlcache,我在webviewdiffinishload中遇到了UIWebView的问题,我得到了 if let urlResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(webView.request!)?.response { if (urlResponse as! NSHTTPURLResponse).statusCode == 200 { } } 由于nil,因此我无法在显示主体时检查状态代码。请问问题

我在
webviewdiffinishload
中遇到了
UIWebView
的问题,我得到了

if let urlResponse = NSURLCache.sharedURLCache().cachedResponseForRequest(webView.request!)?.response {

    if (urlResponse as! NSHTTPURLResponse).statusCode == 200 {

    }
}
由于
nil
,因此我无法在显示主体时检查状态代码。请问问题出在哪里?我可以从服务器端做些什么吗


编辑:从其他请求中我可以看到响应。

因此,我认为这是因为您正在检查
NSURLCache
以获取请求响应。但有时,您的页面可以显示,但不能缓存。实际上,服务器可以做出如下响应:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
它说:“你绝对不应该缓存我的回应”。作为一个好公民,iOS不会:)。因此,我们必须使用不同的方法来获取服务器响应。我认为最好的解决方案是使用
NSURLSession
提出请求,下载内容并转发到您的webview。当发出请求时,您将能够访问完成处理程序的答案。下面是一个您可以做的示例:

func loadURL(url: NSURL!) {
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let muableRequest = NSMutableURLRequest(URL: url)
    muableRequest.setValue("WhateverYouWant", forHTTPHeaderField: "x-YourHeaderField")

    let task = session.dataTaskWithRequest(muableRequest) { (data, response, error) in

        guard error == nil else {
            print("We have an error :( \(error)")
            // Here, you have to handle the error ...
            return
        }

        if let response = response {
            var mimeType = ""
            if response.MIMEType != nil {
                mimeType = response.MIMEType!
            }

            var encoding = ""
            if response.textEncodingName != nil {
                encoding = response.textEncodingName!
            }

            if let httpResponse = response as? NSHTTPURLResponse {
                print("HTTP Status code is \(httpResponse.statusCode)")
            }

            self.webView.loadData(data!, MIMEType: mimeType, textEncodingName: encoding, baseURL: url)
        }
    }
    task.resume()
}
你这样称呼这个方法:

loadURL(NSURL(string: "https://apple.com")!)
在你之前做这件事的地方:

webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://apple.com")!))

您的url已加载意味着可见或不可见not@Anbu.Karthik对不起,我不明白你的意思,你说的可见或不可见是什么意思?它总是发生吗?还是只在某个URL上?@julienquer在某个URL上发生这种情况之前,我想我理解这个问题。您正在检查
NSURLCache.sharedullcache().cachedResponseForRequest
,但是如果答案没有被缓存呢?如果服务器明确表示:“不要缓存此响应”,页面将显示,但不会缓存。感谢您的回答,我现在将尝试。我现在得到的只是这两个标题<代码>缓存控制:公共,最大年龄=3600
过期时间:2016年8月25日星期四08:44:31 GMT
我们是否应该从服务器添加其他内容?在这种情况下,我们应该使用
dataTaskWithRequest
而不是
dataTaskWithURL
。我刚刚编辑了我的答案以满足您的需要。是的,它正在工作,谢谢。但是你能告诉我当报头返回时,缓存控制:public,max age=3600,过期时间:Thu,2016年8月25日08:44:31 GMT,它应该工作吗?老实说,我真的不知道:/。如果你找到原因,我会很感兴趣的!