之前我們項目里使用了UIWebView,用來加載非本地的網頁鏈接。當用戶使用一段時間之后,出現了白頁問題。我們的鏈接里使用了#跳轉URL。為了解決此問題,我嘗試了以下幾種方法:
1.plist.info里添加Allow Arbitrary Loads
具體做法:在plist.info里添加:App Transport Security Settings,在此項中添加Allow Arbitrary Loads,將值設置為YES。此方法是解決網頁內容中含有http鏈接的問題。
嘗試方法1之后,我們的頁面依然白頁。
2.在加載網頁時,忽略本地緩存
代碼如下:
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[self.webView loadRequest:request];
嘗試方法2之后,我們的頁面依然白頁。
3.改變緩存策略
代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
清理緩存:
// Clear All Cookies
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:lastReq];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
嘗試方法3之后,我們的頁面依然白頁。
4.在plist中設置Exception Domains
image
具體做法請見:https://github.com/facebook/react-native/issues/12757
嘗試方法4之后,我們的頁面依然白頁。
5.將UIWebView換成WKWebView
嘗試方法5之后,將UIWebView換成WKWebView,我們的頁面依然白頁。
6.完全刪除網頁產生的緩存
網頁在本地會產生的緩存,主要存在于:
image.png
在 Library下的Caches, Cookies, WebKit文件下,將這三個文件夾刪除即可。如果Caches有你自己的文件,則過濾出來,將其他刪除即可。
參考代碼:
- (void)clearAllUIWebViewData {
// Clear cache...
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self removeCachesWithout:@"data"];
[self removeLibraryDirectory:@"WebKit"];
// Clear cookie
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
[self removeLibraryDirectory:@"Cookies"];
}
- (void)removeLibraryDirectory:(NSString *)dirName {
NSString *dir = [[[[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) lastObject]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:dirName];
if ([[NSFileManager defaultManager] fileExistsAtPath:dir]) {
[[NSFileManager defaultManager] removeItemAtPath:dir error:nil];
}
}
+ (void)removeCachesWithout:(NSString*)dirName {
if (dirName.length < 1) {
// 如果沒有過濾的文件或文件夾,則將整個Caches刪掉
[self removeLibraryDirectory:@"Caches"];
return;
}
NSString *dir = [[[[NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) lastObject]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"Caches"];
if ([[NSFileManager defaultManager] fileExistsAtPath:dir]) {
NSDirectoryEnumerator *myDirectoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:dir];
NSString *file;
while((file = [myDirectoryEnumerator nextObject])) {
// 遍歷當前目錄
if([file isEqualToString:dirName]) {
// 如果需要過濾掉data文件夾不要被刪除
} else {
[[NSFileManager defaultManager] removeItemAtPath:[dir stringByAppendingPathComponent:file] error:nil];
}
}
}
}
這個刪除起了作用,網頁可以顯示了。
如果你開發過程中出現白頁問題,不妨試試以上方法。