[iOS學(xué)習(xí)]WKWebView類的學(xué)習(xí)

WKWebView是在iOS8中發(fā)布的新的Web視圖,旨在替換iOS中的UIWebView和macOS中的WebView。WKWebView很好的解決了UIWebView的內(nèi)存占用大和加載速度慢的問題。

WKWebView可以加載本地HTML代碼或者網(wǎng)絡(luò)資源

//加載網(wǎng)絡(luò)資源時(shí)我們一般采用的是異步的加載方式,則使用以下這個(gè)方法來加載:
//該方法需要的參數(shù)是NSURLRequest對象,必須要嚴(yán)格遵守某種協(xié)議
//日常上網(wǎng)的時(shí)候我們會(huì)把網(wǎng)址前的http://給省略,這個(gè)就是HTTP協(xié)議,在這里絕對不能省略
//由于采用異步請求加載網(wǎng)絡(luò)資源,所以還要實(shí)現(xiàn)相應(yīng)的WKNavigationDelegate委托協(xié)議。請求加載網(wǎng)絡(luò)資源的不用階段會(huì)觸發(fā)委托對象不同的方法
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;

//加載本地資源一般用同步方式,數(shù)據(jù)可以來源于本地文件或者是硬編碼的的HTML字符串,相關(guān)方法如下:
//只用這兩個(gè)方法時(shí),我們需要注意字符集問題,而采用什么字符集取決于HTML文件
- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;

- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL API_AVAILABLE(macosx(10.11), ios(9.0));

下面這個(gè)案例展示了WKWebView的三種方法的用法,我通過三個(gè)按鈕改變?nèi)N加載方式

#import "ViewController.h"
#import <WebKit/WebKit.h>

@interface ViewController ()<WKNavigationDelegate>

@property(nonatomic,strong)WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect screen = [UIScreen mainScreen].bounds;
    
    ///按鈕欄
    //按鈕欄寬度
    CGFloat buttonBarWidth = 316;
    UIView *buttonBar = [[UIView alloc]initWithFrame:CGRectMake((screen.size.width - buttonBarWidth)/2, 20, buttonBarWidth, 30)];
    [self.view addSubview:buttonBar];
    
    ///添加LoadHTMLString按鈕
    UIButton *buttonLoadHTMLString = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadHTMLString setTitle:@"LoadHTMLString" forState:UIControlStateNormal];
    buttonLoadHTMLString.frame = CGRectMake(0, 0, 117, 30);
    [buttonLoadHTMLString addTarget:self action:@selector(testLoadHTMLString:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadHTMLString];
    
    ///添加LoadData按鈕
    UIButton *buttonLoadData = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadData setTitle:@"LoadData" forState:UIControlStateNormal];
    buttonLoadData.frame = CGRectMake(137, 0, 67, 30);
    [buttonLoadData addTarget:self action:@selector(testLoadData:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadData];
    
    ///添加LoadRequest按鈕
    UIButton *buttonLoadRequest = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadRequest setTitle:@"LoadRequest" forState:UIControlStateNormal];
    buttonLoadRequest.frame = CGRectMake(224, 0, 92, 30);
    [buttonLoadRequest addTarget:self action:@selector(testLoadRequest:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadRequest];
    
    //添加WKWebView
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 60, screen.size.width, screen.size.height - 80)];
    
    [self.view addSubview:self.webView];
    
}

- (void)testLoadHTMLString:(id)sender{
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
    NSURL *bundleURl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSError *error = nil;
    
    NSString *html = [[NSString alloc]initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error];
    if (error == nil) {
        [self.webView loadHTMLString:html baseURL:bundleURl];
    }
}

- (void)testLoadData:(id)sender{
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]];
    NSData *htmlData = [[NSData alloc]initWithContentsOfFile:htmlPath];
    
    [self.webView loadData:htmlData MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:bundleUrl];
}

- (void)testLoadRequest:(id)sender{
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.navigationDelegate = self;
}

#pragma mark - WKNavigationDelegate委托協(xié)議
//開始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified 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 withError:(NSError *)error{
    NSLog(@"加載失敗 error:%@",error.description);
}

@end

在iOS9中Xcode引入了新的特性,在LoadRequest方法中請求網(wǎng)絡(luò)資源時(shí),必須使用HTTPS協(xié)議,但是很多情況下我們所訪問的都是HTTP協(xié)議,所以需要去Info.plist文件中修改屬性。如下圖所示修改:

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,407評論 4 61
  • WkWebView是IOS8中引入的新組件,蘋果將UIWebViewDelegate 與 UIWebView 重構(gòu)...
    i_belive閱讀 5,164評論 1 25
  • 很多人都喜歡著不敢上前,卻甘心望著ta背影,說是因?yàn)橄矚g或愛。。。 如果真的喜歡,真的愛,會(huì)甘愿只望著背影嗎? 勇...
    貓絲兒閱讀 900評論 0 3
  • 芽孢桿菌屬微生物特性,芽孢桿菌屬,革蘭氏陽性菌,需氧或兼性厭氧菌產(chǎn)生芽孢,大多數(shù)無夾饃。 利...
    杰克高閱讀 754評論 0 0
  • 本故事純屬虛構(gòu),如有雷同,實(shí)屬倒霉╮(╯╰)╭ 宋仁少時(shí),性平而善,曾隨父母步行翻山至親家。 事畢,急返家,時(shí)值冬...
    拉菲小優(yōu)閱讀 477評論 12 8

友情鏈接更多精彩內(nèi)容