渝湘酒仙
渝湘酒仙
  • 发布:2015-07-31 10:55
  • 更新:2017-10-12 10:02
  • 阅读:3014

iOS原生调用JS

分类:5+ SDK

iOS,用Widget方式集成,原生怎么调JS?
比如,我的js文件中有个test()方法,点击原生界面中的一个按钮,去调用js代码中的test()方法。

2015-07-31 10:55 负责人:无 分享
已邀请:
DCloud_IOS_XTY

DCloud_IOS_XTY

/**  
 @brief 在当前页面同步执行Javascript  
 @param js javasrcipt 脚本  
 @return NSString* 执行结果  
 */  
- (NSString*)stringByEvaluatingJavaScriptFromString:(NSString*)js;
shimeifang

shimeifang

请问解决了吗?怎么通过原生oc调用促发JS中的方法啊,求参考

  • 3***@qq.com

    pragma mark - 封装成js代码+执行回调 JSCommand.h


    typedef NS_ENUM(NSUInteger, JSCommandExecuteStatus) {

    JSCommandExecuteStatusSucess,

    JSCommandExecuteStatusFailed

    };


    static NSString *EvaluateJSNotification = @"com.kmvc.evaluatejs";


    @interface JSCommand : NSObject

    @property (nonatomic, strong) NSString *code;

    @property (nonatomic, copy) void (^callback)(JSCommandExecuteStatus state);



    • (instancetype)initWithCode:(NSString *)code callback:(void (^)(JSCommandExecuteStatus state))callback;

      @90837




    import "JSCommand.h"


    @implementation JSCommand



    • (instancetype)initWithCode:(NSString *)code callback:(void (^)(JSCommandExecuteStatus state))callback {

      self = [super init];

      if (self) {

      self.code = code ? code:@"";

      self.callback = callback;

      }

      return self;

      }

      @90837




    在H5的ViewController里面实现


    pragma mark - 调用js代码




    • (void)evaluateJSCode:(JSCommand )command {

      PDRCore
      core = [PDRCore Instance];

      PDRCoreAppFrame frame = core.appManager.activeApp.mainFrame;

      [frame evaluateJavaScript:command.code completionHandler:^(id obj, NSError
      err) {

      NSLog(@"----- js info ----- %@", obj);

      if (err) {

      UIAlertController alert = [UIAlertController alertControllerWithTitle:@"系统错误" message:[NSString stringWithFormat:@"%@", err] preferredStyle:UIAlertControllerStyleAlert];

      UIAlertAction
      okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

      [alert dismissViewControllerAnimated:true completion:nil];

      if (command.callback) {

      command.callback(JSCommandExecuteStatusFailed);

      }

      }];

      [alert addAction:okAction];

      [self presentViewController:alert animated:true completion:nil];

      return;

      }

      if (command.callback) {

      command.callback(JSCommandExecuteStatusSucess);

      }

      }];

      }




    • (void)needExecuteJsCode:(NSNotification )notification {

      JSCommand
      command = notification.object;

      if (command && [command isKindOfClass:[JSCommand class]]) {

      [self evaluateJSCode:command];

      }

      }







    • (void)loadView

      {

      。。。。。。。。

      // 其他代码

      selector:@selector(needExecuteJsCode:) name:EvaluateJSNotification object:nil];

      }


    使用方法:


    NSString code = @"mui.openWindow({ url: 'hello.html', id: 'hello', extra: {'info':'ok'}});";

    JSCommand
    command = [[JSCommand alloc] initWithCode:code callback:^(JSCommandExecuteStatus state) {

    switch (state) {

    case JSCommandExecuteStatusSucess:


                    break;  
    case JSCommandExecuteStatusFailed:

    break;
    default:
    break;
    }
    [self getBack];
    }];

    [[NSNotificationCenter defaultCenter] postNotificationName:EvaluateJSNotification object:command];

    2017-10-11 16:02

  • shimeifang

    请问我用的是PDRCoreRunModeNormal方式集成,可以获取 PDRCoreAppFrame 对象,使用stringByEvaluatingJavaScriptFromString原生调JS吗,试了很多方法还是不行,不知道什么原因,大神能指导我一下吗

    2017-10-12 10:02

shimeifang

shimeifang

请问我用的是PDRCoreRunModeNormal方式集成,可以获取 PDRCoreAppFrame 对象,使用stringByEvaluatingJavaScriptFromString原生调JS吗,试了很多方法还是不行,不知道什么原因,大神能指导我一下吗

该问题目前已经被锁定, 无法添加新回复