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 

相关文章
相关标签/搜索