从html调用Obj-C方法

时间:2017-09-08 12:32:35

标签: ios objective-c cordova

我从混合开发开始。我试图从我的html按钮调用一个ojbective c方法。

我创建了我的Bridge类:

#import <Foundation/Foundation.h>
#import <JavaScriptCore/JSExport.h>

@protocol BSMThirdPartyIntegrationsBridgeNEWProtocol <JSExport>

-(void) executeNativeMethod: (NSString*) operation json: (NSString*) jsonParams;

@end

@interface BSMThirdPartyIntegrationsBridgeNEW : NSObject <BSMThirdPartyIntegrationsBridgeNEWProtocol>

@end




#import "BSMThirdPartyIntegrationsBridgeNEW.h"

@implementation BSMThirdPartyIntegrationsBridgeNEW

-(void)executeNativeMethod:(NSString *)operation json:(NSString *)jsonParams{

}

@end

然后,在我的网页浏览中,我这样做了:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self showLoadingHUD];
    // get JSContext from UIWebView instance
    JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

    [jsContext setObject:_bSMThirdPartyIntegrationsBridgeNEW.self forKeyedSubscript:@"NativeMethods"];

    // enable error logging
    [jsContext setExceptionHandler:^(JSContext *context, JSValue *value) {
        NSLog(@"WEB JS: %@", value);
    }];
}

最后,我的html文件包含:

<div>
    <button onclick="NativeMethods.executeNativeMethod('sign', '')">Sign</button>
</div>

为什么我的executeNativeMethod没有被调用?

由于

0 个答案:

没有答案