基於PhoneGap3.4架構的iOS外掛程式的實現

來源:互聯網
上載者:User

標籤:style   blog   http   tar   com   get   

Phonegap 提供了iOS 裝置的基礎特性介面來供HTML頁面調用,但是這些基礎介面不能滿足我們的一些特殊需求,所以有時候我們需要開發外掛程式來擴充其功能。基於PhoneGap3.4架構的iOS外掛程式開發,主要分以下幾個步驟:

1)搭建PhoneGap3.4的iOS開發環境,搭建步驟參考此文章。

2)編寫.h標頭檔,範例程式碼如下:

#import <Cordova/CDVPlugin.h>

@interface CDVLogin : CDVPlugin
    - (void)login:(CDVInvokedUrlCommand*)command;
@end

3)編寫.m原始碼檔案,範例程式碼如下:

#import "CDVLogin.h"

@implementation CDVLogin
   - (void)login:(CDVInvokedUrlCommand*)command{
    NSString *echo = @"NIL";
     //外掛程式返回值
    CDVPluginResult *pluginResult = nil;
    //擷取傳遞過來的參數
    NSString *param = [command.arguments objectAtIndex:0];
   Boolean  loginStatus = [self  loginSystem:param];
    if(loginStatus){
        echo = @"YES";
 //成功時外掛程式的返回值
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
    }else{
        echo = @"NO";
 //失敗時外掛程式的返回值
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:echo];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
   
}
-(Boolean) loginSystem:(NSString *)para
{
    return YES;
}
@end

4)在config.xml 裡註冊外掛程式,如下:

<feature name="Login">
        <param name="ios-package" value="CDVLogin"/>
    </feature>

5)js調用,如下:

var CustomPlugin = {
    callNativeMethod: function (success, fail, param) {
        var exec = cordova.require("cordova/exec");
        return exec(success, fail,"Login","login",[param]);
    }
};

function callNativePlugin( param ) {
    CustomPlugin.callNativeMethod( nativePluginResultHandler, nativePluginErrorHandler, param );
}
function nativePluginResultHandler (result) {
    // alert("SUCCESS: \r\n" + result);
}
function nativePluginErrorHandler (error) {
    if(error == "NO"){
        alert("調用失敗!");
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.