cordova常用组件及用法梳理
概述
config.xml:
1  | <plugin name="cordova-plugin-camera" spec="^4.0.3" />  | 
表示当前安装的插件。
使用Cordova CLI 跨平台安装插件
可在官网查询组件
事件触发必须在deviceready事件之后
1  | var app = {  | 
相机
安装
1  | cordova plugin add cordova-plugin-camera  | 
示例
html:
1  | <button id = "cameraTakePicture">照相机</button>  | 
拍照功能:
添加事件监听器:
1  | document.getElementById("cameraTakePicture").addEventListener("click", cameraTakePicture);  | 
调用由插件API提供的 navigator.camera全局对象。拍摄成功回调函数onSuccess
1  | function cameraTakePicture() {  | 

相册功能:
添加事件监听器:
1  | document.getElementById("cameraGetPicture").addEventListener("click", cameraGetPicture);  | 
1  | function cameraGetPicture() {  | 
主要参数
| 参数 | 描述 | 
|---|---|
| quality | 图像质量在0-100范围内。 默认值为50。 | 
| destinationType | DATA_URL 或 0 返回base64编码字符串。 | 
FILE_URI 或 1 返回图片文件URI。
NATIVE_URI 或 2 返回图片本机URI。 |
| sourceType      | PHOTOLIBRARY 或 0 打开照片库。
CAMERA 或 1 打开本机相机。
SAVEDPHOTOALBUM 或 2 打开已保存的相册。 |
| mediaType       | PICTURE 或 0 仅允许选择图片。
VIDEO 或 1 仅允许视频选择。
ALLMEDIA 或 2 允许选择所有媒体类型。 |
文件系统
安装
1  | cordova plugin add cordova-plugin-file  | 
示例
html:
1  | <button id = "createFile">创建文件</button>  | 
添加事件监听器:
1  | document.getElementById("createFile").addEventListener("click", createFile);  | 
创建文件
设备上的应用程序根文件夹中创建文件。window.requestFileSystem请求文件系统,类型可以是 WINDOW.TEMPORARY 或 WINDOW.PERSISTENT,size是存储所需的字节值
1  | function createFile() {  | 

写文件
请求文件系统,然后创建文件写入器,写入我们分配给 blob 变量的文本
1  | function writeFile() {  | 

读取文件
请求文件系统并获取文件对象,创建 reader 。
1  | function readFile() {  | 


删除文件
1  | function removeFile() {  | 
复制文件
1  | function copyFile() {  | 
上传下载文件
安装
1  | cordova plugin add cordova-plugin-file-transfer  | 
示例
html:
1  | <button id = "uploadFile">上传</button>  | 
事件监听器:
1  | document.getElementById("uploadFile").addEventListener("click", uploadFile);  | 
下载
从服务器下载文件到设备。
1  | function downloadFile() {  | 

上传
将文件上传到服务器
1  | function uploadFile() {  | 
地理位置
安装
1  | cordova plugin add cordova-plugin-geolocation  | 
示例
html:
1  | <button id = "getPosition">当前位置</button>  | 
事件监听器:
1  | document.getElementById("getPosition").addEventListener("click", getPosition);  | 
1  | function getPosition() {  | 

音频
安装
1  | cordova plugin add cordova-plugin-media  | 
示例
html:
1  | <button id = "playAudio">播放</button>  | 
事件监听器:
1  | document.getElementById("playAudio").addEventListener("click", playAudio);  | 
播放
1  | var myMedia = null;  | 

暂停和停止
1  | function pauseAudio() {  | 
音量控制
setVolume 方法。此方法采用参数,其值从 0 到 1 。将设置起始值为 0.5
1  | var volumeValue = 0.5;  | 
其他方法
| 方法 | 描述 | 
|---|---|
| getDuration | 返回音频的持续时间 | 
| startRecord | 开始录制音频文件 | 
| stopRecord | 停止录制音频文件 | 
媒体捕获
安装
1  | cordova plugin add cordova-plugin-media-capture  | 
示例
html:
1  | <button id = "audioCapture">录音</button>  | 
事件监听器:
1  | document.getElementById("audioCapture").addEventListener("click", audioCapture);  | 
录音
1  | function audioCapture() {  | 

拍照
1  | function imageCapture() {  | 

摄像
1  | function videoCapture() {  | 

白名单
创建新的Cordova项目时,默认情况下会安装并实施白名单插件。
Navigation Whitelist
导航白名单。允许链接到一些外部URL。config.xml 中配置
1  | <allow-navigation href = "http://example.com/*" />  | 
Intent Whitelist
意向白名单。用于指定允许打开系统的URL。 config.xml 中看到Cordova已经允许我们使用大部分所需的链接。
1  | <allow-intent href="http://*/*" />  | 
Network Request Whitelist
网络请求白名单。
config.xml 文件中,有< Access origin ="\*"/>元素。此元素允许对应用程序的所有网络请求。如果只想允许特定请求,可以自行设置
1  | <access origin="*" />  | 
1  | <access origin = "http://example.com" />  | 
Content Security Policy
内容安全策略。位于 index.HTML 中的 head 元素中。
默认:
1  | <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">  | 
允许来自同源和 foo.com的所有内容:
1  | <meta http-equiv = "Content-Security-Policy" content = "default-src 'self' foo.com">  | 
允许所有内容,但将CSS和javascript限制同源:
1  | <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">  | 
获取当前应用版本号
安装
1  | cordova plugin add cordova-plugin-app-version  | 
示例
1  | cordova.getAppVersion.getVersionNumber(function (version) {  | 
| 方法 | 含义 | 
|---|---|
| getAppName | app名 | 
| getPackageName | 包名 | 
| getVersionCode | app版本码 | 
| getVersionNumber | app版本号 | 
网络信息
安装
1  | cordova plugin add cordova-plugin-network-information  | 
示例
通过navigator.connection.type获取网络类型,取值如下:
| 常量 | 含义 | 
|---|---|
| Connection.UNKNOWN | 未知 | 
| Connection.ETHERNET | 以太网 | 
| Connection.WIFI | wifi | 
| Connection.CELL_2G | 2G蜂窝 | 
| Connection.CELL_3G | 3G蜂窝 | 
| Connection.CELL_4G | 4G蜂窝 | 
| Connection.CELL | 蜂窝 | 
| Connection.NONE | 无网络 | 
另有offline事件和online事件。
1  | document.addEventListener("offline", yourCallbackFunction, false);  | 
1  | document.addEventListener("online", yourCallbackFunction, false);  | 
弹窗
提供四种原生弹窗样式。
安装
1  | cordova plugin add cordova-plugin-dialogs  | 
navigator.notification.alert
1  | navigator.notification.alert(message, alertCallback, [title], [buttonName])  | 
message为弹窗消息,
alertCallback为弹窗消失后的回调函数,
title为弹窗标题,默认为Alert
buttonName为按钮名,默认为OK
navigator.notification.confirm
1  | navigator.notification.alert(message, confirmCallback, [title], [buttonLabels])  | 
message为弹窗消息,
confirmCallback为弹窗消失后的回调函数,参数为点击的按钮index,例如1,2,直接关闭参数为0
title为弹窗标题,默认为Confirm
buttonLabels为按钮标签数组,默认为[OK,Cancel]
navigator.notification.prompt
1  | navigator.notification.prompt(message, promptCallback, [title], [buttonLabels], [defaultText])  | 
navigator.notification.beep
发出蜂鸣声
1  | navigator.notification.beep(times);  | 
设备信息
安装
1  | cordova plugin add cordova-plugin-device  | 
API
| 参数 | 含义 | 
|---|---|
| device.cordova | Cordova版本 | 
| device.model | 设备型号 | 
| device.platform | 平台 | 
| device.uuid | UUID | 
| device.version | 设备版本 | 

首屏加载
安装
1  | cordova plugin add cordova-plugin-splashscreen  | 
示例
config.xml中会出现如下:
1  | <splash src="res/screen/ios/Default~iphone.png" width="320" height="480"/>  | 
这些是默认的首屏加载图,分别对应不同的屏幕大小。可以任意替换这些资源。
屏幕方向
安装
1  | cordova plugin add cordova-plugin-screen-orientation  | 
示例
1  | // 锁定方向  | 
API
| 值 | 含义 | 
|---|---|
| default | 默认 | 
| landscape | 横屏 | 
| portait | 竖屏 | 
退出应用
安装
1  | cordova plugin add cordova-plugin-exitapp  | 
示例
1  | navigator.app.exitApp();  | 
状态栏
安装
1  | cordova plugin add cordova-plugin-statusbar  | 
用法
在config.xml中进行配置
1  | <preference name="StatusBarOverlaysWebView" value="true" />  | 
在启动时是否使状态栏覆盖WebView
1  | <preference name="StatusBarBackgroundColor" value="#000000" />  | 
背景色
1  | <preference name="StatusBarStyle" value="lightcontent" />  | 
样式,可选default, lightcontent, blacktranslucent, blackopaque
1  | <preference name="StatusBarDefaultScrollToTop" value="false" />  | 
是否让Cordova WebView获取默认的scroll-to-top行为
其他插件
| 插件 | 描述 | 
|---|---|
| cordova-plugin-battery-status | 电池状态 | 
| cordova-plugin-contacts | 联系人 |