美文网首页
ios加载html,html中有按钮,点击事件demo摘录

ios加载html,html中有按钮,点击事件demo摘录

作者: 那一处风景ljz | 来源:发表于2018-08-31 16:08 被阅读253次

    html代码

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    </head>
    <body>
    <div style="margin-top: 20px">
    <h2>JS与OC交互</h2>
    <input type="button" value="唤起本地方法(call)" onclick="tianbai.call()">
    </div>
    <div>
    <input type="button" value="唤起getCall:(NSString *)callString传值" onclick="call()">
    </div>
    <script>  
    var call = function()
    {
        var callInfo = JSON.stringify({"jianshu": "http://www.jianshu.com/users/55c8fdc3c6e7/latest_articles"});
            tianbai.getCall(callInfo);
    }
    
    var Callback = function(str)
    {
        alert(str);
    }
    var alerCallback = function()
    {
        alert('成功');
    }
    </script>
    </body>
    </html>
    

    .h文件

    #import <UIKit/UIKit.h>
    #import <JavaScriptCore/JavaScriptCore.h>
    @protocol JSObjcDelegate <JSExport>
    - (void)call;
    - (void)getCall:(NSString *)callString;
    @end
    
    @interface ViewController : UIViewController<UIWebViewDelegate,JSObjcDelegate>
    @property (nonatomic, strong) JSContext *jsContext;
    @property (strong, nonatomic)  UIWebView *webView;
    
    @end
    

    .m文件

    #import "ViewController.h"
    @interface ViewController ()
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    -(void)viewDidAppear:(BOOL)animated{
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        self.webView.delegate = self;
        
        NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
        NSURL* url = [NSURL fileURLWithPath:path];
        NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
        [self.webView loadRequest:request];
        
        [self.view addSubview:self.webView];
    }
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
        self.jsContext[@"tianbai"] = self;
        self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
            context.exception = exceptionValue;
            NSLog(@"异常信息:%@", exceptionValue);
        };
    }
    - (void)call{
        NSLog(@"call");
        // 之后在回调js的方法Callback把内容传出去
        JSValue *Callback = self.jsContext[@"Callback"];
        //传值给web端
        [Callback callWithArguments:@[@"唤起本地OC回调完成"]];
    }
    - (void)getCall:(NSString *)callString{
        NSLog(@"Get:%@", callString);
        // 成功回调js的方法Callback
        JSValue *Callback = self.jsContext[@"alerCallback"];
        [Callback callWithArguments:nil];
        
    //    直接添加提示框
    //    NSString *str = @"alert('OC添加JS提示成功')";
    //    [self.jsContext evaluateScript:str];
    
    }
    @end
    

    相关文章

      网友评论

          本文标题:ios加载html,html中有按钮,点击事件demo摘录

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