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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、UIWebView的基础使用 以上是IOS中UIWebView的基础使用要点详解,接下来一些UIWebView...
    朝雨晚风阅读 1,392评论 0 11
  • 一、简介 近两年随着HTML5的迅速发展与日趋成熟,越来越多的移动开发者选择使用HTML5来进行混合开发,不仅节约...
    RainyGY阅读 1,904评论 1 12
  • 文/钱逊 行己有耻,有所不为。子贡问曰:“何如斯可谓之士矣?”子曰:“行己有耻,使于四方,不辱君命,可谓士矣。”“...
    秦东魁阅读 518评论 0 0
  • 1 关于积累,荀子早在《劝学篇》里就说了“不积跬步,无以至千里;不积小流,无以成江海。”现代人也常说“千里之行,始...
    动动笔记阅读 198评论 0 1
  • 我不知道婚姻对于女生来说意味着什么?身边有好多过了三十岁依然单身的女孩,她们中间,有一个过的明媚俏丽,潇洒不羁,今...
    蜜罐m阅读 103评论 0 0