首页 > 技术文章 > oc 通过webView调用js方法

sun-wsh 2016-08-04 16:36 原文

- (void)viewDidLoad {

    [super viewDidLoad];

//加载本地web页面

    web = [[UIWebView alloc]init];

    web.backgroundColor = [UIColor clearColor];

    web.delegate =self;

    //找到Something.html文件的路径   *不能直接加载x.js文件。可以在x.html里引用x.js或者直接把x.js里的方法放x.html里。

    NSString *basePath =[[NSBundle mainBundle] pathForResource:@"Something" ofType:@"html"];

    NSURL *url = [NSURL fileURLWithPath:basePath];

    [web loadRequest:[NSURLRequest requestWithURL:url]];

}

-(void)clickBtn{

        NSString * strJSMeth = [NSString stringWithFormat:@"GetSomething();"];//调用web中的js方法

        NSString * str = [web stringByEvaluatingJavaScriptFromString:strJSMeth];

        NSLog(@"%@",str);

}

推荐阅读