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];
    // 加載網(wǎng)頁
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
    // 加載doc
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"面向?qū)ο驝.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];
    
    // 自動(dòng)檢測電話號(hào)碼、網(wǎng)址、郵件地址
    self.webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
    // 縮放網(wǎng)頁
    self.webView.scalesPageToFit = YES;
}

@end

2. 與JS交互

1). OC調(diào)用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;
    
    // 設(shè)置代理
    self.webView.delegate = self;
}

#pragma mark 代理方法
// 等待網(wǎng)頁加載完成 才能執(zhí)行js
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 調(diào)用網(wǎng)頁中的函數(shù)
    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 >baidu</a>
<br />
 123@abc.com
  <br />
 http://www.baidu.com
  <div id="container">
  </div>

</body>
</html>

2). JS調(diào)用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;
    
    // 設(shè)置代理
    self.webView.delegate = self;
}

#pragma mark 代理方法
// 發(fā)送請求之前, JS調(diào)用OC代碼
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    // 獲取url中的協(xié)議
    NSLog(@"%@", request.URL.scheme);
    
    // 判斷協(xié)議是否是自定義協(xié)議
    if ([request.URL.scheme isEqualToString:@"source"]) {
        NSLog(@"%@", request.URL.pathComponents);
        // 獲取方法名
        NSString *methodName = request.URL.pathComponents[1];
        // 獲取參數(shù)
        NSString *param = request.URL.pathComponents[2];
        
        // 調(diào)用方法
        // 把字符串的方法名稱轉(zhuǎn)換為一個(gè)selector
        SEL method = NSSelectorFromString(methodName);
        if ([self respondsToSelector:method]) {
            // 解決執(zhí)行方法的內(nèi)存泄漏問題
            #pragma clang diagnostic push
            #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            // 調(diào)用放法
            [self performSelector: method withObject:param];
            #pragma mark diagnostic pop
        }
    }
    // 返回NO,所有的請求都不執(zhí)行
    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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、UIWebView的基礎(chǔ)使用 以上是IOS中UIWebView的基礎(chǔ)使用要點(diǎn)詳解,接下來一些UIWebView...
    朝雨晚風(fēng)閱讀 1,457評(píng)論 0 11
  • 一、簡介 近兩年隨著HTML5的迅速發(fā)展與日趨成熟,越來越多的移動(dòng)開發(fā)者選擇使用HTML5來進(jìn)行混合開發(fā),不僅節(jié)約...
    RainyGY閱讀 2,005評(píng)論 1 12
  • 文/錢遜 行己有恥,有所不為。子貢問曰:“何如斯可謂之士矣?”子曰:“行己有恥,使于四方,不辱君命,可謂士矣?!薄?..
    秦東魁閱讀 583評(píng)論 0 0
  • 1 關(guān)于積累,荀子早在《勸學(xué)篇》里就說了“不積跬步,無以至千里;不積小流,無以成江海。”現(xiàn)代人也常說“千里之行,始...
    動(dòng)動(dòng)筆記閱讀 218評(píng)論 0 1
  • 我不知道婚姻對于女生來說意味著什么?身邊有好多過了三十歲依然單身的女孩,她們中間,有一個(gè)過的明媚俏麗,瀟灑不羈,今...
    蜜罐m閱讀 126評(píng)論 0 0

友情鏈接更多精彩內(nèi)容