iOS UIWebView使用

简介: 1. 加载WebView可直接加载.mp4,.pdf,.html,.doc等格式文件。@interface ViewController ()@property (nonatomic, strong) UIWebView *webView;@e...

1. 加载

WebView可直接加载.mp4,.pdf,.html,.doc等格式文件。

@interface ViewController ()
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation ViewController

- (void)loadView {
    self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.backgroundColor = [UIColor whiteColor];
    // 加载网页
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
    // 加载doc
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"面向对象C.doc" withExtension:nil];
    // 加载pdf
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"史提夫乔布斯传.pdf" withExtension:nil];
    // 播放视频
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"qixi.mp4" withExtension:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    // 自动检测电话号码、网址、邮件地址
    self.webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
    // 缩放网页
    self.webView.scalesPageToFit = YES;
}

@end

2. 与JS交互

1). OC调用JS代码

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

@implementation ViewController

- (void)loadView {
    self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView.backgroundColor = [UIColor whiteColor];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.scalesPageToFit = YES;
    
    // 设置代理
    self.webView.delegate = self;
}

#pragma mark 代理方法
// 等待网页加载完成 才能执行js
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 调用网页中的函数
    NSString *string = [webView stringByEvaluatingJavaScriptFromString:@"test();"];
    NSLog(@"----%@", string);
}

index.html

<!DOCTYPE html>

<html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)">
<head>
  <meta name="viewport"  content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type"  content="text/html; charset=utf-8"/>
  <title></title>
  <script>
      function test() {
      return  "abc";
 }
  </script>
</head>
<body>
  <br />  <br /><br />
 11111111111
  <br />abc
  <br />
  <a href="source:///showMessage:/helloworld">HelloWorld!</a>
  <br />
  <a href="http://www.baidu.com">baidu</a>
<br />
 123@abc.com
  <br />
 http://www.baidu.com
  <div id="container">
  </div>

</body>
</html>

2). JS调用OC代码


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

@implementation ViewController

- (void)loadView {
    self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView.backgroundColor = [UIColor whiteColor];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.scalesPageToFit = YES;
    
    // 设置代理
    self.webView.delegate = self;
}

#pragma mark 代理方法
// 发送请求之前, JS调用OC代码
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    // 获取url中的协议
    NSLog(@"%@", request.URL.scheme);
    
    // 判断协议是否是自定义协议
    if ([request.URL.scheme isEqualToString:@"source"]) {
        NSLog(@"%@", request.URL.pathComponents);
        // 获取方法名
        NSString *methodName = request.URL.pathComponents[1];
        // 获取参数
        NSString *param = request.URL.pathComponents[2];
        
        // 调用方法
        // 把字符串的方法名称转换为一个selector
        SEL method = NSSelectorFromString(methodName);
        if ([self respondsToSelector:method]) {
            // 解决执行方法的内存泄漏问题
            #pragma clang diagnostic push
            #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            // 调用放法
            [self performSelector: method withObject:param];
            #pragma mark diagnostic pop
        }
    }
    // 返回NO,所有的请求都不执行
    return YES;
}

// 显示对话框
- (void)showMessage:(NSString *)str {
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    // 弹出的按钮
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"clicker");
    }];
    [vc addAction:action];
    
    [self presentViewController:vc animated:YES completion:nil];
}

@end
目录
相关文章
|
JavaScript 前端开发 Android开发
iOS中UIWebView的使用详解
iOS中UIWebView的使用详解
204 0
iOS中UIWebView的使用详解
|
Android开发 iOS开发 开发工具