vue 項(xiàng)目 iOS WKWebView 加載
1、首先讓前端的同事打一個(gè)包(index.html,static文件包含css、資源文件、js等)導(dǎo)入項(xiàng)目;
:warning: 注意:
把index.html放入項(xiàng)目根目錄下,command+n創(chuàng)建一個(gè)資源文件.bundle,資源文件里也的包含一份 index.html


下面開始代碼:
懶加載WKWebView
引入#import <WebKit/WebKit.h> #import <WebKit/WKWebView.h>
繼承 WKNavigationDelegate,WKUIDelegate,
- (WKWebView *)wkWebView{
if (!_wkWebView) {
//設(shè)置網(wǎng)頁(yè)的配置文件
WKWebViewConfiguration * Configuration = [[WKWebViewConfiguration alloc]init];
//允許視頻播放
if (@available(iOS 9.0, *)) {
Configuration.allowsAirPlayForMediaPlayback = YES;
} else {
// Fallback on earlier versions
}
// 允許在線播放
Configuration.allowsInlineMediaPlayback = YES;
// 允許可以與網(wǎng)頁(yè)交互,選擇視圖
Configuration.selectionGranularity = YES;
// 關(guān)于 WKWebView 無(wú)法跳轉(zhuǎn)新頁(yè)面 設(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指代的對(duì)象需要遵守WKScriptMessageHandler協(xié)議,結(jié)束時(shí)需要移除
[UserContentController addScriptMessageHandler:self name:@"download"];//DownloadPolicy
// 是否支持記憶讀取
Configuration.suppressesIncrementalRendering = YES;
// 允許用戶更改網(wǎng)頁(yè)的設(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;
// 垂直滾動(dòng)
[_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);
//開啟手勢(shì)觸摸
_wkWebView.allowsBackForwardNavigationGestures = YES;
// 設(shè)置 可以前進(jìn) 和 后退
//適應(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]];
}
}
實(shí)現(xiàn)代理方法
// 接收到服務(wù)器跳轉(zhuǎn)請(qǐng)求之后調(diào)用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"接收到服務(wù)器跳轉(zhuǎn)請(qǐng)求----%@",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ā)送請(qǐng)求之前,決定是否跳轉(zhuǎn)
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"在發(fā)送請(qǐng)求之前,決定是否跳轉(zhuǎn)---%@",navigationAction.request.URL.absoluteString);
//允許跳轉(zhuǎn)
decisionHandler(WKNavigationActionPolicyAllow);
//不允許跳轉(zhuǎn)
//decisionHandler(WKNavigationActionPolicyCancel);
}
#pragma mark - WKNavigationDelegate
// 頁(yè)面開始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"頁(yè)面開始加載");
}
// 當(dāng)內(nèi)容開始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
NSLog(@"內(nèi)容開始返回");
}
// 頁(yè)面加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"頁(yè)面加載完成");
}
// 頁(yè)面加載失敗時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"頁(yè)面加載失敗");
}
如果是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 項(xiàng)目 iOS WKWebView 加載,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- iOS WKWebView適配實(shí)戰(zhàn)篇
- iOs遷至WKWebView跨過的一些坑
- iOS開發(fā)教程之WKWebView與JS的交互
- iOS中WKWebView仿微信加載進(jìn)度條
- 簡(jiǎn)單說說iOS之WKWebView的用法小結(jié)
- iOS中WKWebView的一些特殊使用總結(jié)
- iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決
- iOS和JS交互教程之WKWebView-協(xié)議攔截詳解
- iOS中wkwebView內(nèi)存泄漏與循環(huán)引用問題詳解
- iOS中WKWebView白屏問題的分析與解決
- 微信小程序iOS下拉白屏晃動(dòng)問題解決方案
- iOS WKWebview 白屏檢測(cè)實(shí)現(xiàn)的示例
相關(guān)文章
關(guān)于Vue中keep-alive的作用及使用方法
keep-alive是Vue的內(nèi)置組件,當(dāng)它包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,該組件將不會(huì)銷毀,這篇文章主要介紹了關(guān)于Vue中keep-alive的作用是什么?怎么使用,需要的朋友可以參考下2023-04-04
vue3如何實(shí)現(xiàn)錨點(diǎn)定位點(diǎn)擊滾動(dòng)高亮
這篇文章主要介紹了vue3如何實(shí)現(xiàn)錨點(diǎn)定位點(diǎn)擊滾動(dòng)高亮問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
ant design vue導(dǎo)航菜單與路由配置操作
這篇文章主要介紹了ant design vue導(dǎo)航菜單與路由配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-10-10

