1,首先让前端的同事打一个包(index . html,静态文件包含css,资源文件,js等)导入项目;
把index . html放入项目根目录下,命令+ n创建一个资源文件。束,资源文件里也的包含一份index . html
下面开始代码:
懒加载WKWebView
引入<代码> # import & lt; WebKit/WebKit.h># import & lt; WebKit/WKWebView.h> 代码>
继承<代码> WKNavigationDelegate, WKUIDelegate 代码>,
——(WKWebView *) WKWebView { 如果(! _wkWebView) {//设置网页的配置文件 WKWebViewConfiguration *配置=[[WKWebViewConfiguration alloc] init);//允许视频播放 如果(@available (iOS 9.0 *)) { 配置。allowsAirPlayForMediaPlayback=是的; 其他}{//回退> - (void) viewDidLoad { (超级viewDidLoad); NSFileManager *文件管理=[NSFileManager defaultManager]; NSArray * array1=NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,是的); NSString * matPath2=[[array1, objectAtIndex: 0] stringByAppendingPathComponent: @“QueHTML”);; 如果(!(文件管理fileExistsAtPath: matPath2)) { NSString * matString=[[NSBundle mainBundle] pathForResource: @减低:“QueHTML”@“束”); 设置(dispatch_get_global_queue (0, 0), ^ { (文件管理removeItemAtPath: matPath2错误:nil); (文件管理copyItemAtPath: matString toPath: matPath2错误:nil); 设置(dispatch_get_main_queue ()、^ { NSLog(@“创建完了”); 如果([[[UIDevice currentDevice] systemVersion] floatValue] & lt;9.0){ (自我ios8Load); } 其他{ (自我ios9Load); } }); }); } 其他{ 如果([[[UIDevice currentDevice] systemVersion] floatValue] & lt; 9.0) { (自我ios8Load); } 其他{ (自我ios9Load); } } } - (void) ios8Load { NSArray *路径=NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,是的); NSString=*路径(路径,objectAtIndex: 0); NSString * basePath=[NSString stringWithFormat: @ % @/% @,路径,@“QueHTML/"); (自我。wkWebView loadRequest:(NSURLRequest requestWithURL:(NSURL fileURLWithPath: [NSTemporaryDirectory () stringByAppendingPathComponent: [NSString stringWithFormat: @“www/QueHTML/index . html "]]]]]; } - (void) ios9Load { NSArray *路径=NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask,是的); NSString=*路径(路径,objectAtIndex: 0); NSString * basePath=[NSString stringWithFormat: @ % @/% @,路径,@“QueHTML/"); NSString * htmlPath=[NSString stringWithFormat: @ % @/% @,路径,@“QueHTML/index . html”); NSURL * fileURL=[NSURL fileURLWithPath htmlPath):; 如果(@available (iOS 9.0 *)) { (自我。wkWebView loadFileURL: fileURL allowingReadAccessToURL: [NSURL fileURLWithPath: basePath isDirectory:是的]]; } }
//接收到服务器跳转请求之后调用 - (void) webView: (WKWebView *) webView didReceiveServerRedirectForProvisionalNavigation:导航(WKNavigation *) { NSLog(@“接收到服务器跳转请求- - - - - % @”,导航); }//在收到响应后,决定是否跳转 - (void) webView: (WKWebView *) webView decidePolicyForNavigationResponse: (WKNavigationResponse *) navigationResponse decisionHandler: decisionHandler(空白(^)(WKNavigationResponsePolicy)) { NSLog(@”在收到响应后,决定是否跳转- - - % @”,navigationResponse.response.URL.absoluteString);//允许跳转 decisionHandler (WKNavigationResponsePolicyAllow);//不允许跳转//decisionHandler (WKNavigationResponsePolicyCancel); }//在发送请求之前,决定是否跳转 - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavigationAction *) navigationAction decisionHandler: decisionHandler(空白(^)(WKNavigationActionPolicy)) { NSLog(@”在发送请求之前,决定是否跳转- - - % @”,navigationAction.request.URL.absoluteString);//允许跳转 decisionHandler (WKNavigationActionPolicyAllow);//不允许跳转//decisionHandler (WKNavigationActionPolicyCancel); } # pragma马克- WKNavigationDelegate//页面开始加载时调用 - (void) webView: (WKWebView *) webView didStartProvisionalNavigation:导航(WKNavigation *) { NSLog(@”页面开始加载”); }//当内容开始返回时调用 - (void) webView: (WKWebView *) webView didCommitNavigation:导航(WKNavigation *) { NSLog(@”内容开始返回”); }//页面加载完成之后调用 - (void) webView: (WKWebView *) webView didFinishNavigation:导航(WKNavigation *) { NSLog(@”页面加载完成”); }//页面加载失败时调用 - (void) webView: (WKWebView *) webView didFailProvisionalNavigation:导航(WKNavigation *) { NSLog(@”页面加载失败”); }vue项目iOS WKWebView加载