1.缓存策略详见API
public enum NSURLRequestCachePolicy : UInt {
//缓存策略定义在 web 协议实现中,用于请求特定的URL。是默认的URL缓存策略
case UseProtocolCachePolicy
//从服务端获取数据,忽略本地缓存
case ReloadIgnoringLocalCacheData
//不仅忽略本地的缓存数据,还忽略中间网络媒介(如代理服务器)忽略缓存。直接从最原始的服务器拿取
case ReloadIgnoringLocalAndRemoteCacheData // Unimplemented
//被NSURLRequestReloadIgnoringLocalCacheData替换了
Replaced by NSURLRequestReloadIgnoringLocalCacheData.
public static var ReloadIgnoringCacheData: NSURLRequestCachePolicy { get }
//已经存在的缓存数据用于请求返回,不管它的过期日期和已经存在了多久。如果没有请求对应的缓存数据,从数据源读取
case ReturnCacheDataElseLoad
//已经存在的缓存数据用于请求返回,不管它的过期日期和已经存在了多久。如果没有请求对应的缓存数据,不要去数据源读取,该请求被设置为失败,这种情况多用于离线模式
case ReturnCacheDataDontLoad
//已经存在的缓存数据先去数据源验证有效性,如果无效,将从数据源获取
case ReloadRevalidatingCacheData // Unimplemented
}
2.上部分关键代码
class BaseWebViewController: UIViewController,UIWebViewDelegate {
/// UIWebView
lazy var webView:UIWebView
= {
let web = UIWebView(frame: CGRectZero).then({
let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(self.requestURL!)
var request = NSMutableURLRequest(URL: self.requestURL!)
//判断请求方式
if self.requestURL?.absoluteString.containsString(API_shoppingCart) == true {
request.HTTPMethod = "POST"
}else{
request.HTTPMethod = "GET"
}
//缓存策略定义在 web 协议实现中,用于请求特定的URL。是默认的URL缓存策略
request.cachePolicy = .UseProtocolCachePolicy
//对象代表一个HTTP cookie。 这是一个不可改变的对象,从一个包含cookie的属性的字典初始化,这个类可以用来手动创建cookie的Properties 。
request.HTTPShouldHandleCookies = true
request.allHTTPHeaderFields = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
$0.loadRequest(request)
})
return web
}()
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool{
//判断是否为购物车URL
if request.URL?.absoluteString == API_shoppingCart {
//获取session_id
let session_id = (NSUserDefaults.standardUserDefaults().valueForKey("session_id") as? String) ?? ""
var shoppingCart = ""
//判断登录状态 1为未登录,然后相应根据session_id拼接URL
if let user_id = HLCheckUserID(true) == 1 {
shoppingCart = API_shoppingCart + "&session_id=\(session_id.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)" + "&user_id="
}else{
shoppingCart = API_shoppingCart + "&session_id=\(session_id.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!)" + "&user_id=\(HLCheckUserID(true))"
}
let newRequest = NSMutableURLRequest(URL: NSURL(string: shoppingCart)!)
let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(NSURL(string: shoppingCart)!)
newRequest.HTTPMethod = "POST"
newRequest.cachePolicy = .UseProtocolCachePolicy
newRequest.HTTPShouldHandleCookies = true
newRequest.allHTTPHeaderFields = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
webView.loadRequest(newRequest)
return false
}
return true
}
func webViewDidFinishLoad(webView: UIWebView){
//对cookie进行缓存处理
saveSessionIDCookie()
}
//对cookie进行缓存处理
func saveSessionIDCookie() {
let cookiesToSet = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(requestURL!)!
var tempCookies: [NSHTTPCookie] = []
for cookie in cookiesToSet {
let params = [NSHTTPCookieValue: cookie.value, NSHTTPCookieName: cookie.name, NSHTTPCookiePath: cookie.path, NSHTTPCookieDomain: cookie.domain, NSHTTPCookieExpires: NSDate(timeIntervalSinceNow: 3600*24*30)]
let tempCookie = NSHTTPCookie(properties: params)
tempCookies.append(tempCookie!)
}
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(tempCookies, forURL: requestURL!, mainDocumentURL: nil)
}
}