[关闭]
@Rookie 2019-03-18T02:35:55.000000Z 字数 2649 阅读 1021

定制班车原生-亳州js

panda


  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  4. <script language="javascript">
  5. <!-- // 调用原生登录方法 -->
  6. function loginClick() {
  7. AndroidWebView.loginAction();
  8. }
  9. <!-- // 调用原生支付 -->
  10. function payClick() {
  11. <!-- // 订单号 金额 订单信息 支付方式(wxpay:微信支付 aliPay: 支付宝支付 pursePay:钱包余额支付) -->
  12. AndroidWebView.payAction('201511120981234','3','wxpay','单程车票');
  13. }
  14. <!-- // 调用原生退款查询 -->
  15. function refundClick() {
  16. <!-- // 订单号 退款金额 -->
  17. AndroidWebView.refundAction('B201511120981234','10');
  18. }
  19. <!-- // 登录成功返回 用户ID 登录ID 名字 -->
  20. function loginResult(userId,loginId,name) {
  21. var content = userId+","+loginId+","+name;
  22. asyncAlert(content);
  23. document.getElementById("returnValue").value = content;
  24. }
  25. <!-- // 支付点ID 支付金额 支付结果code 支付渠道 信息 -->
  26. function payResult(orderid,amount,resultCode,channel,message) {
  27. var content = resultCode+","+channel+","+message+","+orderid+","+amount;
  28. asyncAlert(content);
  29. document.getElementById("returnValue").value = content;
  30. }
  31. <!-- // 退款结果 refundCode 0失败 1成功 -->
  32. function refundResult(refundCode,message) {
  33. var content = refundCode+","+message;
  34. asyncAlert(content);
  35. }
  36. function asyncAlert(content) {
  37. setTimeout(function(){
  38. alert(content);
  39. },1);
  40. }
  41. </script>
  42. </head>
  43. <body>
  44. <h1>这是按钮调用</h1>
  45. <input type="button" value="未登录调用" onclick="loginClick()" />
  46. <input type="button" value="支付" onclick="payClick()" />
  47. </body>
  48. </html>

支付返回错误码

9000 支付成功
6001 支付取消
8000 等待支付结果确认
5000 支付失败,请稍后再试

ios - JS相互调用 使用

定义delegate

  1. @protocol JSObjcDelegate <JSExport>
  2. // 调用的JavaScript方法,必须声明!!!
  3. // 无参数定义
  4. - (void)loginAction;
  5. // 有参数定义
  6. JSExportAs(payAction,
  7. - (void)payAction:(NSString *)orderid amount:(NSString *)amount info:(NSString *)info);
  8. @end

生命webView 和 context

  1. @interface WebViewController ()<UIWebViewDelegate,JSObjcDelegate>
  2. @property (nonatomic, strong) JSContext *context;
  3. @property (strong, nonatomic) UIWebView *webView;
  4. @end

在webViewDidFinishLoad 代理里面加载

  1. #pragma mark - UIWebViewDelegate
  2. - (void)webViewDidFinishLoad:(UIWebView *)webView
  3. {
  4. NSLog(@"webViewDidFinishLoad");
  5. self.context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  6. self.context[@"BoZhouJS"] = self;
  7. self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
  8. context.exception = exceptionValue;
  9. NSLog(@"异常信息:%@", exceptionValue);
  10. };
  11. }

实现代理方法

  1. #pragma mark -- JSObjcDelegate
  2. - (void)loginAction {
  3. NSLog(@"登录");
  4. NSString *jsStr = [NSString stringWithFormat:@"loginResult('%@','%@','%@')",@"userId",@"loginId",@"name"];
  5. [[JSContext currentContext] evaluateScript:jsStr];
  6. // 或者
  7. // [self.context evaluateScript:[NSString stringWithFormat:@"loginResult('%@','%@','%@')",userid,loginid,name]];
  8. }
  9. - (void)payAction:(NSString *)orderid amount:(NSString *)amount info:(NSString *)info {
  10. NSLog(@"支付");
  11. NSString *jsStr = [NSString stringWithFormat:@"payResult('%@','%@','%@')",@"resultCode",@"channel",@"message"];
  12. [[JSContext currentContext] evaluateScript:jsStr];
  13. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注