iOS与JS交互

1.在iOS中更改JS的内容

<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <p id="word">哈哈哈哈哈哈哈哈.哈哈哈呵呵</p>
        <ul>
            <li class="change">你好</li>
            <li>我好</li>
            <li>大家好</li>
            <li>才是真的好</li>
        </ul>
        <img src="img_04.jpg">
    </body>
    
</html>
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    self.webView.delegate = self;
    [self.webView loadRequest:request];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    //删除
    NSString *str1 = @"var word = document.getElementById('word');";
    NSString *str2 = @"word.remove();";
    
    [webView stringByEvaluatingJavaScriptFromString:str1];
    [webView stringByEvaluatingJavaScriptFromString:str2];
    
    //更改
    NSString *str3 = @"var change = document.getElementsByClassName('change')[0];"
                        "change.innerHTML = '好你的哦!';";
    [webView stringByEvaluatingJavaScriptFromString:str3];
    
    //插入
    NSString *str4 = @"var img = document.createElement('img');"
                      "img.src = 'img_04.jpg';"
                      "document.body.appendChild(img);";
    [webView stringByEvaluatingJavaScriptFromString:str4];


2.JS调用iOS代码

<html>
  <head>
     <meta charset="UTF-8">
  </head>
  <body>
      <br>
      <p></p>
      <button onclick="openCamera();">访问相册</button>
      <button onclick="openCamera1();">访问相册</button>
      <button onclick="openCamera2();">访问相册</button>
      <button onclick="openCamera3();">访问相册</button>
      
      <a href="http://baidu.com">百度</a>
      
      <script>
          function openCamera(){
             window.location.href = 'xmg?openCamera?numbers=5';
          }
      function openCamera1(){
          window.location.href = 'xmg://phone';
      }
      function openCamera2(){
          window.location.href = 'xmg://sendMessage';
      }

      </script>
  </body>
</html>
思想:点击了网页的按钮,就可以调用iOS的照相。通处就是JS响应,返回数据给iOS,iOS接收到再处理

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}


- (BOOL)webView:(nonnull UIWebView *)webView shouldStartLoadWithRequest:(nonnull NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@"xmg://"];
    NSUInteger location = range.location;
//    xmg://openCamera
    if (location != NSNotFound) {
        NSString *str = [url substringFromIndex:location + range.length];
        NSLog(@"%@", str);
        SEL sel = NSSelectorFromString(str);
        [self performSelector:sel withObject:nil];
    }
    
    return YES;
}


/** 
 * 访问相册
 */
-(void)openCamera{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
  
}


你可能感兴趣的:(iOS与JS交互)