UIWebview和WKWebview的使用 js交互 本地加载 网络加载

#import "GameWebVC.h"
#import "Header.h"
#import "BaseHttpService.h"
#import <WebKit/WebKit.h>
@interface GameWebVC()<WKNavigationDelegate,UIWebViewDelegate>
{
    UIView *myWebView;
    UIImageView *loadingView;
    UIActivityIndicatorView *activityIndicatorView;
    BOOL isloaded;
}

@end


@implementation GameWebVC


- (void)viewDidLoad {
 
    [super viewDidLoad];
  
//网络加载

    // NSURL *url = [NSURL URLWithString:@"http://ip:port/project_path/index.html"];
  
    //本地加载

  NSString *path = [[NSBundle mainBundle]pathForResource:self.gameName ofType:@"html"  inDirectory:self.gameName];

    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
    
    myWebView = [[WKWebView alloc]initWithFrame: [UIScreen mainScreen].bounds];
        
    ((WKWebView *)myWebView).navigationDelegate=self;
    
    [(WKWebView *)myWebView loadRequest:req];
       //其他文件夹下的js加载不到。。
   }else{
       // uiwebview 性能较差
    myWebView = [[UIWebView alloc]initWithFrame: [UIScreen mainScreen].bounds];
       
    ((UIWebView *)myWebView).delegate=self;
       
    [(UIWebView *)myWebView loadRequest:req];
       
   }
    
    [myWebView setUserInteractionEnabled:YES];//是否支持交互
    
    [self.view addSubview:myWebView];
    
    
    self.automaticallyAdjustsScrollViewInsets =NO;
    
    float w = [UIScreen mainScreen].bounds.size.width;
    float h = [UIScreen mainScreen].bounds.size.height;
  
    loadingView =  [[UIImageView alloc]initWithFrame:CGRectMake(w *0.3,(h -  w *0.4 *324 /687)/2, w*0.4,  w *0.4 *324 /687) ];
    loadingView.image = [UIImage imageNamed:@"game_loading"];
    [self.view addSubview:loadingView];
    
   
    activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(w/2.0-50, (h + w *0.4 *324 /687)/2,100, 100)];
   
    [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
   
    [self.view  addSubview:activityIndicatorView];
  
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(910 * ScalW, 0 * ScalH, 90 * ScalH, 90 * ScalH);
    [backButton setImage:[UIImage imageNamed:@"cancel"] forState:(UIControlStateNormal)];
    
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:backButton];
}

//返回按钮
- (void)backAction:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - WKNavigationDelegate

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/**
 *  当内容开始返回时调用
 *
 *  @param webView    实现该代理的webview
 *  @param navigation 当前navigation
 */
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
     [activityIndicatorView startAnimating];
  
    NSLog(@"%s", __FUNCTION__);
}

#pragma mark wkwebview
/**
 *  页面加载完成之后调用
 *
 *  @param webView    实现该代理的webview
 *  @param navigation 当前navigation
 */
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    NSLog(@"%s", __FUNCTION__);
    isloaded = true;
    [loadingView removeFromSuperview];
    [activityIndicatorView stopAnimating];
    //js
    
    [webView evaluateJavaScript:@"postStr('4','4');" completionHandler:^(id any, NSError * _Nullable error) {
        NSLog(@"%@",any);
    } ];
    
    
    
}

/**
 *  加载失败时调用
 *
 *  @param webView    实现该代理的webview
 *  @param navigation 当前navigation
 *  @param error      错误
 */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    
    NSLog(@"%s", __FUNCTION__);
}
#pragma mark uiwebview
-(void)webViewDidStartLoad:(UIWebView *)webView{
     [activityIndicatorView startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [loadingView removeFromSuperview];
    [activityIndicatorView stopAnimating];
    [webView stringByEvaluatingJavaScriptFromString:@"postStr('4','4');"];
    
}

@end
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
    __weak typeof(self) weakSelf = self;
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"goHisBack"] = ^() { // 后退
        // NSArray *args = [JSContext currentArguments];
        [weakSelf JSCallOCWithGoHisBack];
    };
    context[@"Jumpomment"] = ^() { // 跳转评论
        NSArray *args = [JSContext currentArguments];
        [weakSelf JSCallOCWithJumpomment:args];
    };
    context[@"openShare"] = ^() { // 打开分享
        NSArray *args = [JSContext currentArguments];
        [weakSelf JSCallOCWithOpenShare:args];
    };
    
    context[@"showAlert"] = ^() { // 点赞提示
        NSArray *args = [JSContext currentArguments];
        [weakSelf JSCallOCWithShowAlert:args];
    };
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值