iOS 技巧

iOS - js oc相互调用

2021-08-06  本文已影响0人  gaookey

首先导入 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相互调用

上一篇下一篇

猜你喜欢

热点阅读