html端调用oc端方法,比如网页端需要支付,但只有oc才能调起支付宝或微信支付,可以这样写:
OC端:
- 引入库
#import <JavaScriptCore/JavaScriptCore.h>
- 设置代理
@protocol JSObjectProtocol <JSExport>
- (void)callIosPayFunction:(NSString *)orderId;//代理方法
@end
@property (nonatomic, strong) JSContext *jsContext;
- 用UIWebView展示html页面
- 在webViewDidFinishLoad方法里设置对象
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
self.jsContext[@"iosPay"] = self;//“iosPay”相当于和html通信的一个暗号
self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"异常信息:%@", exceptionValue);
};
- 实现代理方法:
- (void)callIosPayFunction:(NSString *)orderId
{
PayForFuwuShangViewController *payV = [[PayForFuwuShangViewController alloc] init];
payV.orderId = orderId;
[self.navigationController pushViewController:payV animated:YES];
}
HTML端:
- 只需一句话,在合适的地方调用代理方法即可
iosPay.callIosPayFunction(result.trade_no);//iosPay就是在oc中设置的暗号