Cordova webapp實戰開發:(6)如何寫一個iOS下獲取APP版本號的插件?

上一篇咱們學習了如何寫一個Andorid下自動更新的插件,我想還有一部分看本系列blog的開發人員但願學習在iOS下如何作插件的吧,那麼今天你就能夠來看看這篇文字了。html

本次練習你能學到的

  1. 學習如何獲取iOS當前版本號
  2. 學習iOS下插件類的編寫
  3. 學習iOS下插件的配置
  4. 學習iOS下插件的調用

主要內容 

  • APP中【檢查更新】顯示當前版本號

插件類的編寫

在上一篇介紹Andorid插件時咱們貼出了不少源碼,這裏也直接貼出代碼,首先是iOS下插件的代碼。前端

咱們在Plugins下新建兩個文件,一個頭文件 CDVGcapp.h,一個實現文件 CDVGcapp.m。(文件名本身取,這是我在項目中的名稱)java

  • CDVGcapp.h
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>

@interface CDVGcapp : CDVPlugin
- (void)version:(CDVInvokedUrlCommand*)command;@end
  • CDVGcapp.m
#import "CDVGcapp.h"
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVScreenOrientationDelegate.h>

@implementation CDVGcapp

- (void)version:(CDVInvokedUrlCommand*)command
{
    NSString* value0 = [NSString stringWithFormat:@"%@(%@)", [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"] ,[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey]];
    
    CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value0];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

 

Javascript如何獲得插件調用後的返回結果?主要經過相似  [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 代碼返回ios

CDVPluginResult,失敗和成功均可以觸發Javascript執行對應的自定義函數

插件的配置

插件寫完了,不少人遇到的下一個問題就是怎麼配置才能在Javascript中調用呢?咱們今天也不解析源碼,爲何呢?由於我沒看:)不過,我必定要給你們說清楚如何配置,不然就永遠調用不了插件。apache

打開staging/config.xml文件,添加feature,必須匹配類名,由於源碼中是經過這些去配對的。上面咱們寫了更新插件,如今就是要配置一下這個插件類到功能名稱,我在配置文件中加入了下文粗體部份內容app

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.glodon.gcapp" version="2.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <preference name="AllowInlineMediaPlayback" value="false" />
    <preference name="AutoHideSplashScreen" value="true" />
    <preference name="BackupWebStorage" value="cloud" />
    <preference name="DisallowOverscroll" value="false" />
    <preference name="EnableViewportScale" value="false" />
    <preference name="FadeSplashScreen" value="true" />
    <preference name="FadeSplashScreenDuration" value=".25" />
    <preference name="KeyboardDisplayRequiresUserAction" value="true" />
    <preference name="MediaPlaybackRequiresUserAction" value="false" />
    <preference name="ShowSplashScreenSpinner" value="true" />
    <preference name="SuppressesIncrementalRendering" value="false" />
    <preference name="TopActivityIndicator" value="gray" />
    <preference name="GapBetweenPages" value="0" />
    <preference name="PageLength" value="0" />
    <preference name="PaginationBreakingMode" value="page" />
    <preference name="PaginationMode" value="unpaginated" />
    <preference name="AutoHideSplashScreen" value="false" />
    
    <name>zgxxj</name>
    <description> 隨時隨地查找全國最完整最及時的信息價     </description>
    <author email="22626496@qq.com" href="http://www.中國信息價.cn">  周金根    </author>
    <content src="html/scj/scj.html" />
    <access origin="*" />
    

    <feature name="Device">
        <param name="ios-package" value="CDVDevice" />
    </feature>
    <feature name="NetworkStatus">
        <param name="ios-package" value="CDVConnection" />
    </feature>
    <feature name="SplashScreen">
        <param name="ios-package" value="CDVSplashScreen" />
        <param name="onload" value="true" />
    </feature>
    <feature name="InAppBrowser">
        <param name="ios-package" value="CDVInAppBrowser" />
    </feature>
    <feature name="Gcapp">
        <param name="ios-package" value="CDVGcapp" />
    </feature>
    <feature name="BarcodeScanner">
        <param name="ios-package" value="CDVBarcodeScanner" />
    </feature>
</widget>

代碼貼完了,我仍是要再多說一下,ide

  • CDVGcapp是插件類名
  • Gcapp是 feature 名稱,下面你們就知道在哪裏會用到了

以上文件就是告訴cordova,咱們新增了一個Gcapp功能,這個功能會調用咱們的原生插件Java對象,接下來就是Javascript如何能調用到這個類了,最重要的就是這個Gcapp功能名稱。函數

咱們接着就要寫Javascript代碼來調用這個功能了,如何寫呢?繼續往下看,我在assets/www/plugins/下新增目錄並創建了文件gcapp.js,完整路徑是 assets/www/plugins/com.gldjc.guangcaiclient/www/gcapp.js,代碼以下:post

 
cordova.define('com.gldjc.guangcaiclient.gcapp', function(require, exports, module) {
        var exec = require("cordova/exec");

        function Gcapp() {};
        
        Gcapp.prototype.version = function (getversion) {
            exec(getversion, null, 'Gcapp', 'version', []);
        };    
                
        var gcapp = new Gcapp();
        module.exports = gcapp;
});
 

exec是cordova.js中內部的函數,當插件返回 PluginResult.Status.OK 時會執行exec的成功回調函數,若是插件返回的是錯誤,則會執行exec的錯誤回調函數。這裏咱們解釋一下 學習

exec(getversion, null, 'Gcapp', 'version', []);

其中Gcapp就是咱們在上一步驟加的feature名稱,大小寫匹配着寫,經過這個名稱,cordova才能找到調用那個java插件類,而後經過version知道調用這個插件類的哪一個方法,後面[]中則是參數。由於我這個插件不須要參數,因此爲空。

Javascript插件類也配對成功了,那如何調用呢?你能夠直接在html中包括這個js,不過咱們通常會再配置一個js,那就是assets/www/cordova_plugins.js,這樣就不用對每一個插件類都去寫一遍了,cordova會遍歷你的配置去加載它們。

cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.device/www/device.js",
        "id": "org.apache.cordova.device.device",
        "clobbers": [
            "device"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.networkinformation/www/network.js",
        "id": "org.apache.cordova.networkinformation.network",
        "clobbers": [
            "navigator.connection",
            "navigator.network.connection"
        ]
    },
     {
        "file": "plugins/org.apache.cordova.networkinformation/www/Connection.js",
        "id": "org.apache.cordova.networkinformation.Connection",
        "clobbers": [
            "Connection"
        ]
    },
     {
        "file": "plugins/org.apache.cordova.splashscreen/www/splashscreen.js",
        "id": "org.apache.cordova.splashscreen",
        "clobbers": [
            "navigator.splashscreen"
        ]
    },
        {
        "file" : "plugins/org.apache.cordova.camera/www/CameraConstants.js",
        "id" : "org.apache.cordova.camera.Camera",
        "clobbers" : [ "Camera" ]
    },
    {
        "file" : "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js",
        "id" : "org.apache.cordova.camera.CameraPopoverOptions",
        "clobbers" : [ "CameraPopoverOptions" ]
    },
    {
        "file" : "plugins/org.apache.cordova.camera/www/Camera.js",
        "id" : "org.apache.cordova.camera.camera",
        "clobbers" : [ "navigator.camera" ]
    },
    {
        "file" : "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js",
        "id" : "org.apache.cordova.camera.CameraPopoverHandle",
        "clobbers" : [ "CameraPopoverHandle" ]
    },
    {
        "file" : "plugins/com.phonegap.plugins.barcodescanner/www/barcodescanner.js",
        "id" : "com.phonegap.plugins.barcodescanner.barcodescanner",
        "clobbers" : [ "barcodescanner" ]
    },
    {
        "file": "plugins/com.gldjc.guangcaiclient/www/gcapp.js",
        "id": "com.gldjc.guangcaiclient.gcapp",
        "clobbers": [
            "gcapp"
        ]
    }
];
module.exports.metadata = 
// TOP OF METADATA
{
    "org.apache.cordova.device": "0.2.13"
}
// BOTTOM OF METADATA
});
 

file表示咱們去哪裏找腳本插件定義js,id是以前咱們在gcapp.js中開頭cordova.define中寫的標識,cordova經過這個標誌去找到咱們的Javascript插件定義,而clobbers則是咱們在前端經過什麼對象名來調用這個插件。這裏我寫的是gcapp,則後面調用則只須要寫成gcapp.checkUpdate 便可

插件的調用

萬事俱備,只欠東風,大家能夠開始看到結果了,若是從頭到這裏一步成功,那應該仍是蠻興奮的事情吧。

具體前端頁面如何設計我就不說了,個人頁面效果就如本文最前面的圖片,在js中我是這些調用version的

$(document).on("PG_pageinit", function(event) {
    gcapp.version(function(version){
            $("#version").html(version);
    });
});

 

 

PhoneGap App開發 477842664 Cordova App實戰開發2 

相關文章
相關標籤/搜索