博客> iOS简单的本地html与oc的交互
iOS简单的本地html与oc的交互
2019-11-18 21:28 评论:0 阅读:3844 我的逆向倾斜
ios oc JS交互

/------------------------------------------ html --------------------------------------------/ <html> <meta charset="utf-8"/> <body>

&lt;/body&gt;

</html>

/------------------------------------------ OC --------------------------------------------/

  • (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) {

    self.backgroundColor = UIColorFromRGB(0xffffff);
    
    [self cerateUI];

    } return self; }

/----------------------- UIWebView创建 -------------------------/

  • (void)cerateUI { NSString *urlFlie = [[NSBundle mainBundle] pathForResource:@"agreementShow" ofType:@"html"];

    self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds]; NSURL url = [NSURL fileURLWithPath:urlFlie]; NSURLRequest request = [NSURLRequest requestWithURL:url]; [_webView loadRequest:request]; _webView.delegate = self; _webView.scalesPageToFit = YES; [self addSubview:_webView];

    UIScrollView first_tempView = (UIScrollView )[_webView.subviews objectAtIndex:0]; first_tempView.scrollEnabled = NO;

    [_webView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.left.equalTo(@0); make.bottom.equalTo(@0); make.right.equalTo(@0); }]; }

/----------------------- UIWebView代理 -------------------------/

  • (void)webViewDidFinishLoad:(UIWebView )webView { self.jsContent = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; self.jsContent[@"Toyun"] = self; self.jsContent.exceptionHandler = ^(JSContext context, JSValue *exceptionValue) { context.exception = exceptionValue; NSLog(@"异常信息:%@", exceptionValue); };

    JSContext *content = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    content.exceptionHandler = ^(JSContext context, JSValue exception) { NSLog(@"异常信息--------------:%@", exception); };

    //html点击回调1 content[@"agreement1"] = ^(NSString *storeId) { [self.racSubjectAgreement sendNext:@0]; };

    //html点击回调2 content[@"agreement2"] = ^(NSString *storeId) { [self.racSubjectAgreement sendNext:@1];

    }; }

收藏
0
sina weixin mail 回到顶部