EasyJSWebView二次加载时JS无法调用原生代码解决办法
来源:互联网 发布:中国证券软件下载 编辑:程序博客网 时间:2024/05/18 00:29
研究了很长时间,终于解决了,当二次加载页面的时候需要先在shouldStartLoadWithRequest中remove掉第一次加载的webview.然后重新建一个webview.
//
// ReloadAccountVC.m// PayHere
//
// Created by snow on 2017/5/3.
// Copyright © 2017年 com.ghl.PayHere. All rights reserved.
//
#import "ReloadAccountVC.h"
#import "MyJSInterface.h"
#import "EasyJSWebView.h"
@interface ReloadAccountVC ()<MyJSInterfaceDelegate,UIWebViewDelegate>
@property (nonatomic,strong) EasyJSWebView *webView;
@property (nonatomic,strong) EasyJSWebView *mWebV;
@end
@implementation ReloadAccountVC
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
}
- (void)initUI{
TerminalCfg *terminalCfg = [Global shareData].terminalCfg;
_webView = [[EasyJSWebView alloc]initWithFrame:self.view.bounds];
_webView.scalesPageToFit = YES;
_webView.delegate = self;
[_webView goBack];
_webView.scrollView.showsVerticalScrollIndicator= NO;
[self.view addSubview:_webView];
MyJSInterface *jsInterface = [MyJSInterface new];
jsInterface.delegate = self; //这个一定要加上
[_webView addJavascriptInterfaces:jsInterface WithName:@"Android"];NSURL *url = [NSURL URLWithString: @"http://219.92.28.137:18082/oglipg/ProviderInit"];
NSString *body = [NSString stringWithFormat: @"x=xitpt&hidRetailerAcc=%@&hidTerminalId=%@&hidUserName=%@&hidSessionId=%@&hidChannel=androidBase",terminalCfg.retailerAcc,[SessionManager getUserDetails:KEY_TID],[SessionManager getUserDetails:KEY_USR],[SessionManager getUserDetails:KEY_SID]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[_webView loadRequest: request];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)web{
NSLog(@"webViewDidFinishLoad");
}
-(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error{
if([error code] == NSURLErrorCancelled)
return;
NSLog(@"DidFailLoadWithError:%@",error);
}
static BOOL isNotFirstLoad=NO;//定义一个静态变量,判断是否是第二次加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"======url: %@", request.URL.absoluteString);
if (isNotFirstLoad) {
[_webView removeFromSuperview];
_webView = nil;
_webView.delegate = nil;
__weak typeof(self) _weakself = self;
_mWebV = [[EasyJSWebView alloc] initWithFrame:CGRectMake(0, 64, _weakself.view.frame.size.width, _weakself.view.frame.size.height)];
[self.view addSubview:_mWebV];
_mWebV.backgroundColor = [UIColor whiteColor];
_mWebV.delegate = self; //这个一定要加上
MyJSInterface *javascriptInterface = [MyJSInterface new];
javascriptInterface.delegate = self; //这个一定要加上
[_mWebV addJavascriptInterfaces:javascriptInterface WithName:@"Android"];[_mWebV loadRequest:request];
isNotFirstLoad = NO;
return NO;
}
isNotFirstLoad = YES;
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
阅读全文
0 0
- EasyJSWebView二次加载时JS无法调用原生代码解决办法
- EasyJSWebView二次加载无效的解决方案
- js调用原生方法解决办法
- js 调用 Android 原生代码
- js调用Android、ios原生代码
- ReactNative-JS 调用原生方法实例代码
- 23、JS调用Android原生代码方法
- android Js通过wepView调用原生代码
- 原生JS实现DOM加载完成马上执行JS代码
- jquery ajax请求加载一个页面后,无法调用原js代码?
- 原生js懒加载
- IOS 与 Android WebView 中 JS 调用原生代码
- WebView和加载html页面时无法调用alert的解决办法
- XHRHttpRequest原生js代码
- JS原生轮播图代码
- 京东轮播图JS 原生代码
- fragment二次加载地图地图无法定位
- jquery mobile页面跳转 js无法加载的解决办法
- 回溯旅行商问题
- JAVA初窥:RMI
- 常用 SQL Server 规范集锦
- Python在保存或运行时出现SyntaxError: Non-ASCII character '\xe8'
- 3251: 树上三角形
- EasyJSWebView二次加载时JS无法调用原生代码解决办法
- CentOS6.3如何安装Gnome界面
- 网页设计
- 2017年6月4日大连海事大学acm校赛 后记
- 刷题
- package属性及其概念
- Spark-编程执行Spark SQL查询
- mytest 测试ObjectName对象
- Java程序员最常用的Linux命令