iOS-WKWebView加载本地的html(适配iOS8系统)

WKWebView好是好,但是有一点恶心的就是加载本地的html时,iOS8跟iOS9+不一样,还得适配。

因为现在项目中混合开发较多,可能多处使用WebView,所以就把家在本地的这个html封装起来。

.h

#import 
#import 

@interface CommonUtils : NSObject

/**
 *  打开本地网页
 *
 *  @param path 路径
 *  @param webView webView
 */
+ (void)loadLocalPath:(NSString *)path webView:(WKWebView *)webView;

/**
 *  将文件copy到tmp目录(wk打开本地网页的解决方法 8.0)wkwebview8.0系统,不支持加载本地html页面,所以需要用以下方法修复!!
 *
 *  @param fileURL fileURL
 *
 *  @ return
 */

+ (NSURL *)fileURLForBuggyWKWebView8:(NSURL *)fileURL;
@end

.m

#import "CommonUtils.h"

@implementation CommonUtils

+ (void)loadLocalPath:(NSString *)path webView:(WKWebView *)webView {
    
    if(path){
        
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
            
            // iOS9. One year later things are OK.
            NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",path]];
            [webView loadRequest:[NSURLRequest requestWithURL:fileURL]];
            
        } else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
            // iOS8.0 以下的 走普通UIWebview
            NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",path]];
            NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
            [webView loadRequest:request];
            
        } else {
            // iOS8. Things can be workaround-ed
            NSURL *fileURL = [CommonUtils fileURLForBuggyWKWebView8:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@",path]]];
            NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
            [webView loadRequest:request];
        }
    }
}
//iOS8之所以跟iOS9+系统加载方式不同,就差在这个路径。
+ (NSURL *)fileURLForBuggyWKWebView8:(NSURL *)fileURL {
    
    NSError *error = nil;
    if (!fileURL.fileURL || ![fileURL checkResourceIsReachableAndReturnError:&error]) {
        return nil;
    }
    
    // Create "/temp/www" directory
    NSFileManager *fileManager= [NSFileManager defaultManager];
    NSURL *temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];
    [fileManager createDirectoryAtURL:temDirURL withIntermediateDirectories:YES attributes:nil error:&error];
    
    // 取到本地html后的锚点
    NSString *lastPathComponent = [[fileURL.absoluteString componentsSeparatedByString:@"/"] lastObject];
    NSURL *dstURL = [NSURL URLWithString:[temDirURL.absoluteString stringByAppendingString:lastPathComponent]];
    
    // Now copy given file to the temp directory
    [fileManager removeItemAtURL:dstURL error:&error];
    [fileManager copyItemAtURL:fileURL toURL:dstURL error:&error];
    // Files in "/temp/www" load flawlesly :)
    return dstURL;
}
@end

都封装好了,那使用起来就更加简单了。

/**
 <#加载本地html#>

 @param news 这个就是前端提供的html文件名称
 */
NSString *path = [[NSBundle mainBundle]pathForResource:@"news" ofType:@"html"];
[CommonUtils loadLocalPath:path webView:self.webView];
iOS-WKWebView加载本地的html(适配iOS8系统)_第1张图片
本地html文件

这样就大功告成了。

你可能感兴趣的:(iOS-WKWebView加载本地的html(适配iOS8系统))