標籤:des style blog ar io color os 使用 sp
//ios 外掛程式開發//Echo iOS Plugin Example//配置 在config.xml中<platform name="ios"> <config-file target="config.xml" parent="/*"> <feature name="Echo"> <param name="ios-package" value="CDVEcho" /> </feature> </config-file></platform>//Echo 標頭檔#import <Cordova/CDC.h>@interface CDVEcho:CDVPlugin//執行個體方法-(void) echo:(CDVInvokedUrlCommand *) command;@end//實現部分#import "CDVEcho.h"#import <Cordova/CDC.h>@implementation CDVEcho-(void) echo:(CDVInvokedUrlCommand *) command{ CDVPluginResult* pluginResult = nil; NSString* echo = [command.arguments objectAtIndex:0]; if (echo != nil && [echo length] > 0) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; } [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];}@end//線程處理 - (void)myPluginMethod:(CDVInvokedUrlCommand*)command{ // Check command.arguments here. [self.commandDelegate runInBackground:^{ NSString* payload = nil; // Some blocking logic... CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload]; // The sendPluginResult method is thread-safe. [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }];}//^{}:用來等待queue裡的最後一個block被執行掉,常用操作之一//^這個東西,說明一個塊函數,()這個東西是塊裡面需要的參數{}執行體//需要注意的是 weakSelf , 不是直接使用self, 防止循環參考 [self.commandDelegate runInBackground:^{}];JavaScript:1.successFunction:一個成功的回呼函數2.failFunction:一個錯誤回呼函數3.service: 一個本地類名4.action:本地類方法名5.args:數組的參數傳遞到本地環境 exec(<successFunction>, <failFunction>, <service>, <action>, [<args>]); Sample: //定義cordova.define("com.JajaCy.cordova.echo", function(require, exports, module) { var exec = require(‘cordova/exec‘); var platform = require(‘cordova/platform‘); /** * Provides access to Echo on the device. * 在裝置上提供通知。 */ module.exports = { /** * Demo * @param {String} message * @param {Function} completeCallback */ echo: function(message,callback) { exec(callback, function(err){ callback(‘Nothing to echo.‘); }, "Echo", "echo", [message]); } };});//cordova_plugins.js 外掛程式配置cordova.define(‘cordova/plugin_list‘, function(require, exports, module) { module.exports = [{ "file": "plugins/com.JajaCy.cordova.echo/www/echo.js", "id": "com.JajaCy.cordova.echo.echo", "merges": [ "window"//調用方法對象 ] }]; module.exports.metadata = // TOP OF METADATA { "com.JajaCy.cordova.echo": "0.0.1", } // BOTTOM OF METADATA});//調用 window.echo(‘Hello Echo!‘,function(data){ alert(data); });
Cordova+ ios 外掛程式開發