UIWebView,WKWebView白页问题解决

之前我们项目里使用了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];
            }
        }
    }
}

这个删除起了作用,网页可以显示了。

如果你开发过程中出现白页问题,不妨试试以上方法。

你可能感兴趣的:(UIWebView,WKWebView白页问题解决)