javaScriptCore 的使用

首先咱们在HTML里面定义一个标签。html

 <!-- 定义一个事件 把须要传递的视图控制器的名字传过去-->java

            <a id="push" href="#" onclick="native.pushViewControllerWithTitle('CLDetailViewController','hello word');">web

                你敢不敢点我</a>atom

咱们要获取到这事件而且完成跳转。lua

在控制器中咱们要这样写.net

 

#import <UIKit/UIKit.h>htm

#import <JavaScriptCore/JavaScriptCore.h>事件

 

@protocol TestJSExport <JSExport>ip

 

JSExportAs(calculateForJS,/** charlieCalculate 做为 js方法的别名*/ - (void)calculateNumber:(NSNumber*)number);get

//跳转页面

- (void)pushViewController:(NSString*)viewControllerName withTitle:(NSString *)title;

 

 

@end

 

@interface HomeViewController : UIViewController<TestJSExport,UIWebViewDelegate>

 

@property (nonatomic ,strong) UIWebView * myWebview;

@property (nonatomic ,strong) JSContext * context;

 

@end

////////////////

#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    // 以 html title 设置 导航栏 title

    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    // Undocumented access to UIWebView's JSContext

    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    // 打印异常

    self.context.exceptionHandler =

    ^(JSContext *context, JSValue *exceptionValue)

    {

        context.exception = exceptionValue;

        NSLog(@"%@", exceptionValue);

    };

    // 以 JSExport 协议关联 native 的方法

    self.context[@"native"] = self;

    

    // 以 block 形式关联 JavaScript function

    self.context[@"log"] =

    ^(NSString *str)

    {

        NSLog(@"%@", str);

    };

  

}

//页面的跳转

-(void)pushViewController:(NSString *)viewControllerName WithTitle:(NSString *)title{

    

    Class second = NSClassFromString(viewControllerName);

    id secondVc = [[second alloc]init];

    NSLog(@"%@",title);

    ((UIViewController*)secondVc).title = title;

    [self.navigationController pushViewController:secondVc animated:YES];

    

}

相关文章
相关标签/搜索