• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 用 Swift 编写 Cordova/Ionic 插件

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:23:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我按照该站点的说明进行操作,http://moduscreate.com/writing-a-cordova-plugin-in-swift-for-ios/ ,用于为 iOS 平台创建我自己的 cordova 插件。 首先,我安装了plugman 并创建了一个新插件。这是我的 plugin.xml:

<?xml version='1.0' encoding='utf-8'?>
<plugin id="com-moduscreate-plugins-echoswift" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>ModusEchoSwift</name>
<js-module name="ModusEchoSwift" src="www/ModusEchoSwift.js">
    <clobbers target="modusechoswift" />
</js-module>
<platform name="ios">
    <config-file target="config.xml" parent="/*">
        <feature name="ModusEchoSwift">
            <param name="ios-package" value="ModusEchoSwift" />
        </feature>
    </config-file>
    <source-file src="src/ios/ModusEchoSwift.swift" />
</platform>
</plugin>

这是我的插件 js 文件,即 ModusEchoSwift.js:

var exec = require('cordova/exec');
exports.echo = function(arg0, success, error) {
    exec(success, error, "ModusEchoSwift", "echo", [arg0]);
};
exports.echojs = function(arg0, success, error) {
    if (arg0 && typeof(arg0) === 'string' && arg0.length > 0) {
        success(arg0);
    } else {
        error('Empty message!');
    }
};

这是我的原生 Swift 类,即 ModusEchoSwift.swift:

@objc(ModusEchoSwift) class ModusEchoSwift : CDVPlugin {
func echo(command: CDVInvokedUrlCommand) {
    var pluginResult = CDVPluginResult(
        status: CDVCommandStatus_ERROR
    )

    let msg = command.arguments[0] as? String ?? ""

    if msg.characters.count > 0 {
        /* UIAlertController is iOS 8 or newer only. */
        let toastController: UIAlertController = 
            UIAlertController(
                title: "", 
                message: msg, 
                preferredStyle: .Alert
            )

        self.viewController?.presentViewController(
            toastController, 
            animated: true, 
            completion: nil
        )

        let duration = Double(NSEC_PER_SEC) * 3.0
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW, 
                Int64(duration)
            ), 
            dispatch_get_main_queue(), 
            { 
                toastController.dismissViewControllerAnimated(
                    true, 
                    completion: nil
                )
            }
        )

        pluginResult = CDVPluginResult(
            status: CDVCommandStatus_OK,
            messageAsString: msg
        )
    }

    self.commandDelegate!.sendPluginResult(
        pluginResult, 
        callbackId: command.callbackId
    )
  }
}

这些是我插件中的文件。现在我试图在我的 ionic 项目中使用这个插件显示一个简单的警报。我现在有一个简单的 index.html,我想在其中显示警报,这里是:

<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">Ionic Blank Starter</h1>
  </ion-header-bar>
  <ion-content>
    <div ng-controller="myController">
      Hello {{user}}
      </div>
  </ion-content>
</ion-pane>

这是我的 Controller :

.controller('myController', function($scope){
console.log("Ok");
$scope.user = "kAy";
ModusEchoSwift.echo('lugin Ready!', function(msg) { 
   console.log("Success");
  },
  function(err) {
   console.log("Error");
  }
);

})

在此 Controller 中,我不断收到未定义 ModusEchoSwift 的错误,我不明白!谁能告诉我如何在我现有的 ionic 项目中使用我的插件的功能??



Best Answer-推荐答案


确实可以在 ionic v2.2.1 和 cordova v6.5.0 上正常工作,前提是 Simon 的 blog post 中列出的步骤紧随其后,并且有一个小改动:在导入下方的 App.component.ts 文件中添加这行代码:

declare let modusechoswift;

在我的例子中, ionic 应用程序中的 Bridging-Header.h 文件位于“其他来源”下。西蒙的帖子提到了一个不同的位置,这最初让我很反感,但这不是问题。事实上,在我的 Xcode 项目中的任何地方移动该头文件似乎没有任何效果。我猜 XCode 不关心文件在哪里,只要它存在。

关于ios - 用 Swift 编写 Cordova/Ionic 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37237546/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap