iOS与js的互调

1.在iOS中调用HTML

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

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

#pragma mark - 操作网页
-(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_01.jpg';"
                     "img.width = '160';"
                     "img.height = '80';"
                     "document.body.appendChild(img);";
    [webView stringByEvaluatingJavaScriptFromString:str4];
}

@end

index.html


   
      
   
   
      

12122121212112

  • 你好
  • 我好
  • 大家好
  • 才是真的好

2.ios调用HTML2

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
/** <#注释#> */
@property (nonatomic, weak) UIActivityIndicatorView *activityView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载网页
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"xianhua" withExtension:@"html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
//    self.webView.scrollView.hidden = YES;
//    self.webView.backgroundColor = [UIColor grayColor];
//    
//    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//    // 转起来
//    [activityView startAnimating];
//    activityView.center = self.view.center;
//    [self.webView addSubview:activityView];
//    self.activityView = activityView;
}

#pragma mark - 操作网页
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//   // 改变标题
//    NSString *str1 = @"var h1 = document.getElementsByTagName('h1')[0];"
//                      "h1.innerHTML='大神一期鲜花网';";
//    [webView stringByEvaluatingJavaScriptFromString:str1];
//    
//    //删除尾部
//    NSString *str2 =@"document.getElementById('footer').remove();";
//    [webView stringByEvaluatingJavaScriptFromString:str2];
//    
//    // 让scrollView显示
//    self.webView.scrollView.hidden = NO;
//    self.activityView.hidden = YES;
//    
//    // 拿出所有的网页内容
//    NSString *str3 = @"document.body.outerHTML";
//    NSString *html = [webView stringByEvaluatingJavaScriptFromString:str3];
//    NSLog(@"%@", html);
}

@end

3.在HTML中调用OC
index.html


   
      
   
   
      
      
      
   

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

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

}

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

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

@end

4.在HTML中调用OC


   
      
      
   
   
       

用户名

密码

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

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

}

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



@end

你可能感兴趣的:(iOS与js的互调)