如何从iOS的离子电容器插件获取数据?

时间:2019-01-02 20:05:14

标签: ionic-framework ionic4

我正在创建一个新的带有ionic 4电容器的ios插件。我可以创建该插件并导入到我的项目中,但是我的插件方法存在问题。

我向插件发送someData,希望我的方法响应一个布尔值,但这不会发生。

插件IOS

1).SWIFT

lazy var keychain : KeychainSwift = {
    return KeychainSwift()
}()

@objc func setKeychain(_ call: CAPPluginCall) {
    let value = call.getString("textToSave") ?? ""
    let identifier = call.getString("identifierOfKey") ?? ""
    var response = false

    if !identifier.isEmpty, !value.isEmpty {
        keychain.set(value, forKey: identifier)
        response = true
    }

    call.resolve([
        "value": response
        ])
}

@objc func setKeychainWithBoolean(_ call: CAPPluginCall) {
    let value = call.getString("textToSave") ?? ""
    let identifier = call.getString("identifierOfKey") ?? ""
    var status = call.getBool("status") ?? false

    //if !identifier.isEmpty, !value.isEmpty {
    //    keychain.set(value, forKey: identifier)
    //    status = true
    //}

    status = true

    call.resolve([
        "value": status
        ])
}

2).m

CAP_PLUGIN(DanielPlugin, "DanielPlugin",
       CAP_PLUGIN_METHOD(setKeychain, CAPPluginReturnPromise);
       CAP_PLUGIN_METHOD(setKeychainWithBoolean, CAPPluginReturnPromise);
       CAP_PLUGIN_METHOD(echo, CAPPluginReturnPromise);
)

3)src / definitions.ts

setKeychain(options: { textToSave: string, identifierOfKey: string}): Promise<{value: Object }>;
setKeychainWithBoolean(options: { textToSave: string, identifierOfKey: string , status: boolean }): Promise<{value: Object }>;
echo(options: {value: string}): Promise<{value: string}>;

4)src / web.ts

async setKeychain(options: { textToSave: string, identifierOfKey: string }): Promise<{value: Object }>{
return Promise.resolve({ value: options.textToSave });}

async setKeychainWithBoolean(options: { textToSave: string, identifierOfKey: string, status: boolean }): Promise<{value: Object }>{
return Promise.resolve({ value: options.status });}

async echo(options: {value: string}): Promise<{value: string}> {
return Promise.resolve({ value: options.value });}

离子项目

1)

onSubmit(){
    this.setObject(); 
  }

  async setObject(){

    const nameValue = this.dataText.get('inputName').value; 

    const data = await DanielPlugin.setKeychain({
      textToSave: nameValue,
      identifierOfKey: 'name'
    });

    const otherData = await DanielPlugin.setKeychainWithBoolean({
      textToSave: nameValue,
      identifierOfKey: 'name',
      status: false
    });

    console.log("json response data:",data);
    console.log("json response otherData:",otherData);
  }

在方法“ setKeychain”上,我期望输出为“ true”或“ false”,但是我得到了我输入的名称。

在方法“ setKeychainWithBoolean”上,我期望输出为“ true”,但始终为“ false”

插件-> https://github.com/daniel190392/daniel-plugin-ionic

0 个答案:

没有答案