#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];
};
}