美文网首页
iOS - js oc相互调用

iOS - js oc相互调用

作者: gaookey | 来源:发表于2020-10-01 15:16 被阅读0次

首先导入 JavaScriptCore.framework 框架

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>

@protocol ServerJSProtocol <JSExport>

// 微信登录
- (void)wxLogin;
// 忘记密码
- (void)toForget;
// 去注册
- (void)toReg;

// 登录
- (BOOL)userLogin:(BOOL)nc_vok :(NSString *)csessionid :(NSString *)sig :(NSString *)token :(NSString *)scene :(NSString *)tx_Usermobilenum :(NSString *)tx_UserPassword;

@end

@interface ServerJS : NSObject <ServerJSProtocol>

@end
#import "ServerJS.h"

@implementation ServerJS

- (void)wxLogin { }
- (void)toForget { }
- (void)toReg { }

- (BOOL)userLogin:(BOOL)nc_vok :(NSString *)csessionid :(NSString *)sig :(NSString *)token :(NSString *)scene :(NSString *)tx_Usermobilenum :(NSString *)tx_UserPassword {
    return YES;
}

@end

使用:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    ServerJS *js = [[ServerJS alloc] init];
    context[@"mobileObj"] = js;
}

方法名 和 mobileObj 必须和js里面的一样

附上js部分代码

document.getElementById("bt_login").onclick = function(){
 if (!mobileObj.userLogin(nc_vok,
                          document.getElementById('csessionid').value,
                          document.getElementById('sig').value,
                          document.getElementById('token').value,
                          document.getElementById('scene').value,
                          document.getElementById('tx_Usermobilenum').value,
                          document.getElementById('tx_UserPassword').value)) {
     resetAliyunAfs();
 }
};

document.getElementById("wxlogin").onclick = function(){
 mobileObj.wxLogin();
};

document.getElementById("bt_forget").onclick = function(){
 mobileObj.toForget();
};

document.getElementById("bt_reg").onclick = function(){
 mobileObj.toReg();
};

摘录自本人 开源中国博客: iOS - js oc相互调用

相关文章

网友评论

      本文标题:iOS - js oc相互调用

      本文链接:https://www.haomeiwen.com/subject/hrzpuktx.html