最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue 項(xiàng)目 iOS WKWebView 加載

 更新時(shí)間:2019年04月17日 09:14:33   作者:BestWilliam  
這篇文章主要介紹了vue 項(xiàng)目 iOS WKWebView 加載問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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)頁的配置文件
    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指代的對(duì)象需要遵守WKScriptMessageHandler協(xié)議,結(jié)束時(shí)需要移除
    [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;
    // 垂直滾動(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
// 頁面開始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  NSLog(@"頁面開始加載");
}
// 當(dāng)內(nèi)容開始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  NSLog(@"內(nèi)容開始返回");
}
// 頁面加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  NSLog(@"頁面加載完成");
}
// 頁面加載失敗時(shí)調(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 項(xiàng)目 iOS WKWebView 加載,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • vue如何實(shí)現(xiàn)上傳圖片和顯示圖片

    vue如何實(shí)現(xiàn)上傳圖片和顯示圖片

    這篇文章主要介紹了vue如何實(shí)現(xiàn)上傳圖片和顯示圖片問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Nuxt3:拉取項(xiàng)目模板失敗問題以及解決

    Nuxt3:拉取項(xiàng)目模板失敗問題以及解決

    文章描述了在使用官網(wǎng)命令創(chuàng)建Nuxt3項(xiàng)目時(shí)遇到的問題,通過分析命令,推測(cè)問題出在拉取項(xiàng)目模板失敗,解決方法是手動(dòng)訪問并下載項(xiàng)目模板,解壓后按照官網(wǎng)教程安裝依賴并啟動(dòng),最終成功解決問題
    2024-12-12
  • Vue3配置vite.config.js解決跨域問題的方法

    Vue3配置vite.config.js解決跨域問題的方法

    跨域一般出現(xiàn)在開發(fā)階段,由于線上環(huán)境前端代碼被打包成了靜態(tài)資源,因而不會(huì)出現(xiàn)跨域問題,這篇文章主要給大家介紹了關(guān)于Vue3配置vite.config.js解決跨域問題的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • vue3登錄界面適配的二種方法實(shí)例代碼

    vue3登錄界面適配的二種方法實(shí)例代碼

    這篇文章主要介紹了vue3登錄界面適配的二種方法,在Vue.js中使用計(jì)算屬性和條件渲染來處理,每種方法都有其適用場(chǎng)景,選擇哪種方法取決于具體需求,需要的朋友可以參考下
    2024-12-12
  • vue print.js打印支持Echarts圖表操作

    vue print.js打印支持Echarts圖表操作

    這篇文章主要介紹了vue print.js打印支持Echarts圖表操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 關(guān)于Vue中keep-alive的作用及使用方法

    關(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
  • vue-jsonp的使用及說明

    vue-jsonp的使用及說明

    這篇文章主要介紹了vue-jsonp的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue3如何實(shí)現(xiàn)錨點(diǎn)定位點(diǎn)擊滾動(dòng)高亮

    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
  • 一文詳解Vue-組件自定義事件(綁定和解綁)

    一文詳解Vue-組件自定義事件(綁定和解綁)

    這篇文章主要介紹了Vue-組件自定義事件的綁定和解綁,文中有詳細(xì)的圖文實(shí)例,對(duì)學(xué)習(xí)或工作有一定的參考價(jià)值,需要的小伙伴可以閱讀下
    2023-05-05
  • ant design vue導(dǎo)航菜單與路由配置操作

    ant design vue導(dǎo)航菜單與路由配置操作

    這篇文章主要介紹了ant design vue導(dǎo)航菜單與路由配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評(píng)論

安西县| 西乌| 呼伦贝尔市| 宜兰县| 卓尼县| 鄂尔多斯市| 遵化市| 仙桃市| 新兴县| 吐鲁番市| 壶关县| 霍州市| 如东县| 苍山县| 扎赉特旗| 崇文区| 高邑县| 道孚县| 邓州市| 卢湾区| 泰顺县| 金湖县| 泗水县| 海伦市| 延川县| 郧西县| 西吉县| 赤壁市| 定西市| 东阳市| 眉山市| 瓦房店市| 家居| 青神县| 新竹县| 准格尔旗| 盐边县| 克拉玛依市| 昭通市| 镇原县| 望都县|