@Rookie
2019-03-18T02:35:55.000000Z
字数 2649
阅读 1343
panda
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf8"><script language="javascript"><!-- // 调用原生登录方法 -->function loginClick() {AndroidWebView.loginAction();}<!-- // 调用原生支付 -->function payClick() {<!-- // 订单号 金额 订单信息 支付方式(wxpay:微信支付 aliPay: 支付宝支付 pursePay:钱包余额支付) -->AndroidWebView.payAction('201511120981234','3','wxpay','单程车票');}<!-- // 调用原生退款查询 -->function refundClick() {<!-- // 订单号 退款金额 -->AndroidWebView.refundAction('B201511120981234','10');}<!-- // 登录成功返回 用户ID 登录ID 名字 -->function loginResult(userId,loginId,name) {var content = userId+","+loginId+","+name;asyncAlert(content);document.getElementById("returnValue").value = content;}<!-- // 支付点ID 支付金额 支付结果code 支付渠道 信息 -->function payResult(orderid,amount,resultCode,channel,message) {var content = resultCode+","+channel+","+message+","+orderid+","+amount;asyncAlert(content);document.getElementById("returnValue").value = content;}<!-- // 退款结果 refundCode 0失败 1成功 -->function refundResult(refundCode,message) {var content = refundCode+","+message;asyncAlert(content);}function asyncAlert(content) {setTimeout(function(){alert(content);},1);}</script></head><body><h1>这是按钮调用</h1><input type="button" value="未登录调用" onclick="loginClick()" /><input type="button" value="支付" onclick="payClick()" /></body></html>
9000 支付成功
6001 支付取消
8000 等待支付结果确认
5000 支付失败,请稍后再试
@protocol JSObjcDelegate <JSExport>// 调用的JavaScript方法,必须声明!!!// 无参数定义- (void)loginAction;// 有参数定义JSExportAs(payAction,- (void)payAction:(NSString *)orderid amount:(NSString *)amount info:(NSString *)info);@end
@interface WebViewController ()<UIWebViewDelegate,JSObjcDelegate>@property (nonatomic, strong) JSContext *context;@property (strong, nonatomic) UIWebView *webView;@end
#pragma mark - UIWebViewDelegate- (void)webViewDidFinishLoad:(UIWebView *)webView{NSLog(@"webViewDidFinishLoad");self.context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];self.context[@"BoZhouJS"] = self;self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {context.exception = exceptionValue;NSLog(@"异常信息:%@", exceptionValue);};}
#pragma mark -- JSObjcDelegate- (void)loginAction {NSLog(@"登录");NSString *jsStr = [NSString stringWithFormat:@"loginResult('%@','%@','%@')",@"userId",@"loginId",@"name"];[[JSContext currentContext] evaluateScript:jsStr];// 或者// [self.context evaluateScript:[NSString stringWithFormat:@"loginResult('%@','%@','%@')",userid,loginid,name]];}- (void)payAction:(NSString *)orderid amount:(NSString *)amount info:(NSString *)info {NSLog(@"支付");NSString *jsStr = [NSString stringWithFormat:@"payResult('%@','%@','%@')",@"resultCode",@"channel",@"message"];[[JSContext currentContext] evaluateScript:jsStr];}