UIKit 框架之WebView

时间:2021-05-21 17:02:38
//
//  ViewController.m
//  UIWebView
//
//  Created by City--Online on 15/5/18.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>
@property(nonatomic,strong) UIWebView *webView;
@property(nonatomic,strong) UIActivityIndicatorView *activityView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _webView=[[UIWebView alloc]init];
    _webView.frame=self.view.bounds;
    _webView.delegate=self;

//    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"百度"ofType:@"html"];
//    NSString *str=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//    [_webView loadHTMLString:str baseURL:nil];

    NSURL *url=[NSURL URLWithString:@"http://www.cnblogs.com/gcb999/p/3178728.html"];
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];

    //禁用拖拽时的反弹效果
    _webView.scrollView.bounces=NO;
    //默认值为NO,用户不可以放大或缩小页面;如果设置为YES,页面可以通过放大缩小去适应,用户也可以通过手势来放大和缩小
    _webView.scalesPageToFit=YES;
    //此属性可以设定使电话号码、网址、电子邮件和符合格式的日期等文字变为链接文字
//    typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
//        UIDataDetectorTypePhoneNumber   = 1 << 0,          // Phone number detection 识别电话号码
//        UIDataDetectorTypeLink          = 1 << 1,          // URL detection识别网址,链接等
//        UIDataDetectorTypeAddress       = 1 << 2,          // Street address detection 识别地址
//        UIDataDetectorTypeCalendarEvent = 1 << 3,          // Event detection 识别时间
//        UIDataDetectorTypeNone          = 0,               // No detection at all 全都不识别
//        UIDataDetectorTypeAll           = NSUIntegerMax    // All types 全部识别
//    };
    _webView.dataDetectorTypes=UIDataDetectorTypePhoneNumber;

//    控制webview使用html5的video播放视频不全屏(inline)的方法
//    webview中用html5的video方式播放视频时,在ipad上是默认原来大小的,而在iphone上是默认全屏播放的
//    HTML里video必须加上webkit-playsinline属性
//    <video id="player" width="480" height="320" webkit-playsinline>
//    Obj-C里,webview设置allowsInlineMediaPlayback属性为YES
//    webview.allowsInlineMediaPlayback = YES;
    _webView.allowsInlineMediaPlayback=YES;

    //是否支持自动播放
//    <script>
//    if ("wView" in window) {
//        window.wView.allowsInlineMediaPlayback = "YES";
//        window.wView.mediaPlaybackRequiresUserAction = "NO";
//    }
//    </script>
//    在head中加入此段代码,ios音视频不能自动播放的问题迎刃而解。
//    当然,在video标签中,需要先设定autoplay和preload属性,如下:
//    <video src="xxxxxx" autoplay preload></video>

    _webView.mediaPlaybackRequiresUserAction=NO;

    //从这个页面是否可以Air Play。 在iPhone和iPad上默认使YES。
    _webView.mediaPlaybackAllowsAirPlay=YES;

    //是否网页内容下载完毕才开始渲染web视图,默认为NO
    _webView.suppressesIncrementalRendering=NO;

    //是否在web页面响应用户输入弹出键盘,默认为YES
    _webView.keyboardDisplayRequiresUserAction=YES;

    //IOS7增加了分页功能
//    @property (nonatomic) UIWebPaginationMode paginationMode NS_AVAILABLE_IOS(7_0);
//    @property (nonatomic) UIWebPaginationBreakingMode paginationBreakingMode NS_AVAILABLE_IOS(7_0);
//    @property (nonatomic) CGFloat pageLength NS_AVAILABLE_IOS(7_0);
//    @property (nonatomic) CGFloat gapBetweenPages NS_AVAILABLE_IOS(7_0);
//    @property (nonatomic, readonly) NSUInteger pageCount NS_AVAILABLE_IOS(7_0);

    _webView.paginationMode=UIWebPaginationModeUnpaginated;
    [_webView loadRequest:request];

    [self.view addSubview:_webView];
    _activityView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(20, 20, 40, 40)];
    _activityView.center=self.view.center;
    _activityView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhiteLarge;
    [self.view addSubview:_activityView];

}
//如果返回NO,代表不允许加载这个请求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    switch (navigationType)
    {
            //点击连接
        case UIWebViewNavigationTypeLinkClicked:
        {
            NSLog(@"clicked");
        }
            break;
            //提交表单
        case UIWebViewNavigationTypeFormSubmitted:
        {
            NSLog(@"submitted");
        }
        default:
            break;
    }
    return YES;
}
//开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_activityView startAnimating];
}
//加载完毕
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activityView stopAnimating];
}
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//    [_webView goBack];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

与JS交互这块下面的博客还不错

http://blog.csdn.net/lizhongfu2013/article/details/9232129

http://blog.csdn.net/lizhongfu2013/article/details/9236357

UIKit 框架之WebViewUIKit 框架之WebViewUIKit 框架之WebViewUIKit 框架之WebView