UIWebView - 网页

关于更多网页加载:WKWebKit,Safari,SafariViewController
这里只讲UIWebView,使用简单,性能一般差。

下面的分开讲。
WKWebView:iOS8 新框架:WebKit
SafariViewController:iOS9新框架:SafariServices

还有就是直接调用 Safari:

    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    [[UIApplication sharedApplication] openURL:url];

加载数据

  • 1 url 加载
    NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
    [webView loadRequest:request];//加载
    
  • 2 根据本地html 文本加载
    [webView loadHTMLString:@"htmlString" baseURL:nil];
 
  • 3 加载本地数据 NSData
// 可以加载 pdf,gif,docx  txt等
   NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
    [webView loadData:[NSData dataWithContentsOfFile:path] MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@""]];

    // 更多 MIMEType
    // http://tool.oschina.net/commons?type=22013-05-17

一些属性设置

// 内容显示范围设置
 [webView.scrollView setContentInset:UIEdgeInsetsMake(10, 100, 10, 100)];
    
    webView.scalesPageToFit = YES;// 是否允许缩放?
    webView.dataDetectorTypes = UIDataDetectorTypeAll;  // 自动识别 网址,电话等
    webView.allowsInlineMediaPlayback = YES; // 支持h5 播放
    webView.mediaPlaybackRequiresUserAction = YES;// h5 播放控制
    webView.mediaPlaybackAllowsAirPlay = YES;// 。。。
    webView.suppressesIncrementalRendering = YES;// webView 一次性加载,没读取完成就不加载
    webView.keyboardDisplayRequiresUserAction = YES;// 允许使用键盘
    
    webView.paginationMode = UIWebPaginationModeUnpaginated;
    /*
     typedef NS_ENUM(NSInteger, UIWebPaginationMode) {
     UIWebPaginationModeUnpaginated,
     UIWebPaginationModeLeftToRight,
     UIWebPaginationModeTopToBottom,
     UIWebPaginationModeBottomToTop,
     UIWebPaginationModeRightToLeft
     };翻页模式
     */
    
    webView.paginationBreakingMode = UIWebPaginationBreakingModeColumn;
    /*
     typedef NS_ENUM(NSInteger, UIWebPaginationBreakingMode) {
     UIWebPaginationBreakingModePage,
     UIWebPaginationBreakingModeColumn
     };分页模式
     */
    
    
    // 判断
    if (webView.canGoBack) {
        NSLog(@"goback");
    }
    if (webView.canGoForward) {
        NSLog(@"goforward");
    }
    if (webView.loading) {
        NSLog(@"loading");
    }
    
    // 事件 操作
    //    [webView stopLoading];
    //    [webView reload];
    //    [webView goBack];
    //    [webView goForward];
    
    // iOS 9 的属性
    webView.allowsPictureInPictureMediaPlayback = YES;// 画中画
    webView.allowsLinkPreview = YES;// 3d touch 预览 相关

代理

// 判断,是否允许加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    /*
     typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
     UIWebViewNavigationTypeLinkClicked,
     UIWebViewNavigationTypeFormSubmitted,
     UIWebViewNavigationTypeBackForward,
     UIWebViewNavigationTypeReload,
     UIWebViewNavigationTypeFormResubmitted,
     UIWebViewNavigationTypeOther
     };
     */
    
    if(navigationType == UIWebViewNavigationTypeLinkClicked) {
        //判断是否是点击链接
        return NO;
    } else {
        return YES;
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"start");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finish");
    
    // 也是一些属性内容
    NSLog(@"%f",webView.pageLength);
    NSLog(@"%f",webView.gapBetweenPages);
    NSLog(@"%zi",webView.pageCount);
    NSLog(@"%@",webView.request);
    
    [webView stringByEvaluatingJavaScriptFromString:@""];// 使用js 代码执行操作
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error {
    NSLog(@"error");
}

1

你可能感兴趣的:(UIWebView - 网页)