iOS UIWebView使用

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值