vue 項目 iOS WKWebView 加載
1、首先讓前端的同事打一個包(index.html,static文件包含css、資源文件、js等)導入項目;
:warning: 注意:
把index.html放入項目根目錄下,command+n創(chuàng)建一個資源文件.bundle,資源文件里也的包含一份 index.html
下面開始代碼:
懶加載WKWebView
引入#import <WebKit/WebKit.h> #import <WebKit/WKWebView.h>
繼承 WKNavigationDelegate,WKUIDelegate
,
- (WKWebView *)wkWebView{ if (!_wkWebView) { //設(shè)置網(wǎng)頁的配置文件 WKWebViewConfiguration * Configuration = [[WKWebViewConfiguration alloc]init]; //允許視頻播放 if (@available(iOS 9.0, *)) { Configuration.allowsAirPlayForMediaPlayback = YES; } else { // Fallback on earlier versions } // 允許在線播放 Configuration.allowsInlineMediaPlayback = YES; // 允許可以與網(wǎng)頁交互,選擇視圖 Configuration.selectionGranularity = YES; // 關(guān)于 WKWebView 無法跳轉(zhuǎn)新頁面 設(shè)置 Configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES; // web內(nèi)容處理池 Configuration.processPool = [[WKProcessPool alloc] init]; //自定義配置,一般用于 js調(diào)用oc方法(OC攔截URL中的數(shù)據(jù)做自定義操作) WKUserContentController * UserContentController = [[WKUserContentController alloc]init]; // 添加消息處理,注意:self指代的對象需要遵守WKScriptMessageHandler協(xié)議,結(jié)束時需要移除 [UserContentController addScriptMessageHandler:self name:@"download"];//DownloadPolicy // 是否支持記憶讀取 Configuration.suppressesIncrementalRendering = YES; // 允許用戶更改網(wǎng)頁的設(shè)置 Configuration.userContentController = UserContentController; _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kIs_iPhoneX? self.view.frame.size.height-34:self.view.frame.size.height) configuration:Configuration]; _wkWebView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]; // 設(shè)置代理 _wkWebView.navigationDelegate = self; _wkWebView.UIDelegate = self; // 垂直滾動 [_wkWebView.scrollView setShowsVerticalScrollIndicator:NO]; _wkWebView.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, kIs_iPhoneX? self.view.frame.size.height-34:self.view.frame.size.height); //開啟手勢觸摸 _wkWebView.allowsBackForwardNavigationGestures = YES; // 設(shè)置 可以前進 和 后退 //適應(yīng)你設(shè)定的尺寸 [_wkWebView sizeToFit]; [self.view addSubview:_wkWebView]; } return _wkWebView; }
iOS 9 以后和 iOS 8 之前 加載方法不一樣,做區(qū)分
- (void)viewDidLoad { [super viewDidLoad]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *matPath1 = [[array1 objectAtIndex:0] stringByAppendingPathComponent:@"QueHTML"];; if (![fileManager fileExistsAtPath:matPath1]) { NSString *matString = [[NSBundle mainBundle] pathForResource:@"QueHTML" ofType:@"bundle"]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ [fileManager removeItemAtPath:matPath1 error:nil]; [fileManager copyItemAtPath:matString toPath:matPath1 error:nil]; dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"創(chuàng)建完了"); if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) { [self ios8Load]; } else{ [self ios9Load]; } }); }); } else{ if ([[[UIDevice currentDevice] systemVersion] floatValue] <9.0) { [self ios8Load]; } else{ [self ios9Load]; } } } - (void)ios8Load { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSString *basePath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/"]; [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"www/QueHTML/index.html"]]]]]; } - (void)ios9Load { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSString *basePath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/"]; NSString *htmlPath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/index.html"]; NSURL *fileURL = [NSURL fileURLWithPath:htmlPath]; if (@available(iOS 9.0, *)) { [self.wkWebView loadFileURL:fileURL allowingReadAccessToURL:[NSURL fileURLWithPath:basePath isDirectory:YES]]; } }
實現(xiàn)代理方法
// 接收到服務(wù)器跳轉(zhuǎn)請求之后調(diào)用 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{ NSLog(@"接收到服務(wù)器跳轉(zhuǎn)請求----%@",navigation); } // 在收到響應(yīng)后,決定是否跳轉(zhuǎn) - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{ NSLog(@"在收到響應(yīng)后,決定是否跳轉(zhuǎn)---%@",navigationResponse.response.URL.absoluteString); //允許跳轉(zhuǎn) decisionHandler(WKNavigationResponsePolicyAllow); //不允許跳轉(zhuǎn) //decisionHandler(WKNavigationResponsePolicyCancel); } // 在發(fā)送請求之前,決定是否跳轉(zhuǎn) - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{ NSLog(@"在發(fā)送請求之前,決定是否跳轉(zhuǎn)---%@",navigationAction.request.URL.absoluteString); //允許跳轉(zhuǎn) decisionHandler(WKNavigationActionPolicyAllow); //不允許跳轉(zhuǎn) //decisionHandler(WKNavigationActionPolicyCancel); } #pragma mark - WKNavigationDelegate // 頁面開始加載時調(diào)用 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ NSLog(@"頁面開始加載"); } // 當內(nèi)容開始返回時調(diào)用 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{ NSLog(@"內(nèi)容開始返回"); } // 頁面加載完成之后調(diào)用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{ NSLog(@"頁面加載完成"); } // 頁面加載失敗時調(diào)用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{ NSLog(@"頁面加載失敗"); }
如果是https訪問需加上一下代碼
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { if ([challenge previousFailureCount] == 0) { NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; completionHandler(NSURLSessionAuthChallengeUseCredential, credential); } else { completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); } } else { completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); } }
總結(jié)
以上所述是小編給大家介紹的vue 項目 iOS WKWebView 加載,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- iOS WKWebView適配實戰(zhàn)篇
- iOs遷至WKWebView跨過的一些坑
- iOS開發(fā)教程之WKWebView與JS的交互
- iOS中WKWebView仿微信加載進度條
- 簡單說說iOS之WKWebView的用法小結(jié)
- iOS中WKWebView的一些特殊使用總結(jié)
- iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決
- iOS和JS交互教程之WKWebView-協(xié)議攔截詳解
- iOS中wkwebView內(nèi)存泄漏與循環(huán)引用問題詳解
- iOS中WKWebView白屏問題的分析與解決
- 微信小程序iOS下拉白屏晃動問題解決方案
- iOS WKWebview 白屏檢測實現(xiàn)的示例
相關(guān)文章
vue給input file綁定函數(shù)獲取當前上傳的對象完美實現(xiàn)方法
這篇文章主要介紹了vue給input file綁定函數(shù)獲取當前上傳的對象完美實現(xiàn)方法,需要的朋友可以參考下2017-12-12vue使用el-table篩選tree樹形結(jié)構(gòu)的數(shù)據(jù)問題
這篇文章主要介紹了vue使用el-table篩選tree樹形結(jié)構(gòu)的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07vue?contextmenujs鼠標右鍵菜單高度不夠顯示不全的問題及解決方法
這篇文章主要介紹了使用vue-contextmenujs鼠標右鍵菜單時,當高度不夠時顯示不全的問題,大家需要注意本文給提供的解決方案雖然能夠解決現(xiàn)有問題,但是如果組件升級了,想要使用最新升級后的組件,還要再次修改代碼,需要的朋友可以參考下2022-07-07Vue中JSON文件神奇應(yīng)用fetch、axios異步加載與模塊導入全指南詳細教程
在Vue中使用JSON文件有多種方式,包括使用fetch方法加載JSON文件、使用axios庫加載JSON文件,以及將JSON文件導入為模塊,這篇文章主要介紹了Vue中JSON文件神奇應(yīng)用fetch、axios異步加載與模塊導入全指南詳細教程,需要的朋友可以參考下2024-01-01