如何在离子项目中导入cordova自定义插件?

时间:2017-05-28 15:46:55

标签: ios cordova ionic2 ionic3

我在我的项目ionic 3中添加了我的自定义插件:

离子cordova插件添加/ path /到/ custom / plugin

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

declare var className: any;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {
    className.plugins.ClassName.methodName("======> WORK");
  }
}

的plugin.xml

<js-module src="www/ClassName.js" name="ClassName">
<clobbers target="className" />
</js-module>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="ClassName">
<param name="ios-package" value="ClassName"/>
</feature>
</config-file>
<header-file src="src/ios/ClassName.h" />
<source-file src="src/ios/ClassName.m" />
</platform>

ClassName.js

function ClassName() {
}
ClassName.prototype.methodName = function(echo, successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "ClassName", "methodName", [echo]);
};
module.exports = new ClassName();

ClassName.m

#import "ClassName.h"

@implementation ClassName

-(void)methodName:(CDVInvokedUrlCommand *)command {
NSString* echo = [command.arguments objectAtIndex:0];
NSLog(@"%@", echo);
}
@end

ClassName.h

#import <Cordova/CDVPlugin.h>

@interface ClassName : CDVPlugin
-(void)methodName:(CDVInvokedUrlCommand *)command;
@end

我构建项目并进入xcode尝试,但模拟器只显示黑屏,没有看到NSLog日志... 我怎么解决?导入自定义插件的正确方法是什么?感谢

1 个答案:

答案 0 :(得分:0)

在调用插件之前,您需要确保已加载平台。

使用platform.ready()

constructor(public navCtrl: NavController,public platform:Platform) {
    this.platform.ready().then(()=>{
       className.plugins.ClassName.methodName("======> WORK");
    });
  }