我开发了自定义Cordova plugin
。我可以从JS
成功调用它,但我还需要从另一个类(本机代码)调用它。我该怎么办?
如何调用自定义插件的showImageURL
方法?
// MyCustomPlugin.m
@implementation MyCustomPlugin
- (void) showImageURL:(CDVInvokedUrlCommand*)command{
if (_fullScreenImageView)
return;
NSString *fullPath = [[command.arguments objectAtIndex:0] valueForKey:@"url"];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"www/application/app/%@", fullPath]];
_fullScreenImageView = [[UIImageView alloc] initWithImage:image];
_fullScreenImageView.frame=[[UIScreen mainScreen] bounds];
UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
[controller.view addSubview:_fullScreenImageView];
[controller.view bringSubviewToFront:_fullScreenImageView];
}
// AnotherClass.m
@implementation AnotherClass
- (void) foo {
MyCustomPlugin *splashScreen = [[MyCustomPlugin alloc] init];
[splashScreen showImageURL:]; // <<- what params should I pass to `showImageURL`?
}
P.S。这就是我从JS中调用它的方式:window.FullScreenImage.showImageURL('img/bar.png');
答案 0 :(得分:1)
最好的方法是抽象出Cordova接口,以便将其称为简单的Objective-C函数:
@implementation MyCustomPlugin
- (void) showImageURL:(CDVInvokedUrlCommand*)command{
NSString *fullPath = [[command.arguments objectAtIndex:0] valueForKey:@"url"];
[self _showImageURL:fullPath];
}
- (void) _showImageURL:(NSString*)fullPath{
if (_fullScreenImageView)
return;
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"www/application/app/%@", fullPath]];
_fullScreenImageView = [[UIImageView alloc] initWithImage:image];
_fullScreenImageView.frame=[[UIScreen mainScreen] bounds];
UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
[controller.view addSubview:_fullScreenImageView];
[controller.view bringSubviewToFront:_fullScreenImageView];
}
@implementation AnotherClass
- (void) foo {
MyCustomPlugin *splashScreen = [[MyCustomPlugin alloc] init];
[splashScreen _showImageURL:@"path/to/some/image.png"];
}