在webview上从javascript调用Objective-c函数

在webview上从javascript调用Objective-c函数,javascript,ios,objective-c,uiwebview,Javascript,Ios,Objective C,Uiwebview,最近我做了这个互动,当我在寻找信息的时候,我看到网上没有那么多信息,所以,现在我解决了这个问题,我想与大家分享 应用程序中的第一个 On.h 进口 #import <JavaScriptCore/JavaScriptCore.h> On.m 现在,在你的网页上 函数FuncWithParams(param1){ Delegate.FuncWithParams(param1); } 函数FuncWithoutParams(){ Delegate.FuncWithoutParams

最近我做了这个互动,当我在寻找信息的时候,我看到网上没有那么多信息,所以,现在我解决了这个问题,我想与大家分享

应用程序中的第一个

On.h

进口

#import <JavaScriptCore/JavaScriptCore.h>
On.m

现在,在你的网页上


函数FuncWithParams(param1){
Delegate.FuncWithParams(param1);
}
函数FuncWithoutParams(){
Delegate.FuncWithoutParams();
}
@property (nonatomic, readwrite, strong) JSContext *js;
- (void)viewDidLoad{

     NSString *address = @"http://yourpage.com";

     // Build the url and loadRequest
     NSString *urlString = [NSString stringWithFormat:@"%@",address];
     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];

     //Get JSContext from webview
     self.js = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

     //Set Delegate to JS object
     self.js[@"Delegate"] = self;

     //Set Handlers to events on JS
     self.js[@"Delegate"][@"FuncWithParams"] = ^(NSString *param1) {
         [self FuncWithParams:param1];
     };

     self.js[@"Delegate"][@"FuncWithoutParams"] = ^{
         [self FuncWithoutParams];
     };

}

//Handlers
- (void)FuncWithParams:(NSString *)param1 {
     NSLog(@"Param =%@", param1);
     //Do what you want
}

- (void)FuncWithoutParams {
    NSLog(@"FuncWithoutParams");
    //Do what you want
}