IOS9之后,uiwebview请求网页变慢怎么办?
-EnGirl开发经验分享
1.在ios9之前,用uiwebview请求网页,在ios9之后,可用WKWebView请求网页,这样ios开发中请求网页速度才不会变慢。
2.设备版本判断:[[UIDevicecurrentDevice]systemVersion]
3.如果要调用起js的方法,uiwebview和wkwebview是有区别的:
uiwebview可用:[uiwebview stringByEvaluatingJavaScriptFromString:@"fn();"];
wkwebview可用:[_wkWebview evaluateJavaScript:@"fn();"completionHandler:^(id_Nullablejs_iOS,NSError*_Nullableerror) { }];
4.uiwebview和wkwebview清除缓存、cookie的区别:
uiwebview用下面的方式:
//清除缓存
[[NSURLCachesharedURLCache]removeAllCachedResponses];
NSURLCache* cache = [NSURLCachesharedURLCache];
[cacheremoveAllCachedResponses];
[cachesetDiskCapacity:0];
[cachesetMemoryCapacity:0];
//清除cookie
NSHTTPCookie*cookie;
NSHTTPCookieStorage*storage =[NSHTTPCookieStoragesharedHTTPCookieStorage];
for(cookiein[storagecookies])
{
[storagedeleteCookie:cookie];
}
wkwebview用下面的方式:
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
NSDate*dateFrom = [NSDatedateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStoredefaultDataStore]removeDataOfTypes:websiteDataTypes
modifiedSince:dateFromcompletionHandler:^{
}];