由於 iOS的UIWebview會自動進行緩存,有時候會導致一些授權失敗的問題,遇到這樣的清除Cookie和緩存就好了
對於需要清除緩存的html頁面
if (_isTicket) { NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [storage cookies]){ [storage deleteCookie:cookie]; } //清除UIWebView的緩存 [[NSURLCache sharedURLCache] removeAllCachedResponses]; NSURLCache *cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; [cache setDiskCapacity:0]; [cache setMemoryCapacity:0]; }
對於 和前端h5頁面做交互 緩存的css樣式問題 可以直接在請求的時候 指定緩存策略,不使用緩存
// UIWebView 緩存策略 NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url]; if (_isTicket) { request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request.timeoutInterval = 5.0; }
UIWebView緩存策略
1、NSURLRequestUseProtocolCachePolicy NSURLRequest 默認的cache policy,使用Protocol協議定義。
2、NSURLRequestReloadIgnoringCacheData 忽略緩存直接從原始地址下載。
3、NSURLRequestReturnCacheDataDontLoad 只使用cache數據,如果不存在cache,請求失敗;用於沒有建立網絡連接離線模式
4、NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data時才從原始地址下載。
5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData 忽略本地和遠程的緩存數據,直接從原始地址下載,與NSURLRequestReloadIgnoringCacheData類似。
6、NSURLRequestReloadRevalidatingCacheData 驗證本地數據與遠程數據是否相同,如果不同則下載遠程數據,否則使用本地數據