LoginSignup
13
12

More than 5 years have passed since last update.

Cordovaのプラグイン追加と利用方法

Posted at

はじめに

前回の「Cordovaの環境構築をしてみる」で構築した環境にプラグインの追加と利用方法を記載していきます。

プラグインの追加

ここではdeviceのプラグインを追加します。
「ターミナル」でSampleCordovaフォルダに移動し、以下のコマンドを実行します。

$ cordova plugin add org.apache.cordova.device

インストールが完了したら、インストールされているプラグインを確認します。

$ cordova plugin ls
cordova-plugin-whitelist 1.0.0 "Whitelist"
org.apache.cordova.device 0.3.0 "Device"

whitelistのプラグインはプロジェクト作成時に既に格納されているプラグインです。
追加したdeviceのプラグインがインストールされていることを確認できましたので、
次は実際にプラグインを利用してみます。

プラグインの利用

Deviceプラグインでは、プラグインの初期化時にwindow.deviceに以下の情報が格納されます。

  • device.cordova
  • device.model
  • device.platform
  • device.uuid
  • device.version

アプリ起動後、devicereadyイベントで上記内容をalertで出力するようにindex.jsを変更します。

SampleCordova/www/js/index.js
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);

        // Deviceプラグインで取得した情報をalertで表示
        var deviceInfo = "";
        deviceInfo += "cordova:" + device.cordova + "\n";
        deviceInfo += "model:" + device.model + "\n";
        deviceInfo += "platform:" + device.platform + "\n";
        deviceInfo += "uuid:" + device.uuid + "\n";
        deviceInfo += "version:" + device.version + "\n";

        alert(deviceInfo);
    }
};

app.initialize();

devicereadyイベントは、cordova.jsで発行されるイベントとなります。
プロジェクトで使用している全プラグインの初期化が完了した際にイベントが発行されます。
そのため、devicereadyイベントの前にプラグインを利用すると、まだ利用できない可能性があるためエラーとなる場合があります。

Simulatorを実行します。

$ cordova emulate ios

実行した結果、以下のようになります。
スクリーンショット_2015-09-08_17_37_15.png

アプリ情報、端末情報が取得できていることが確認できます。

その他のプラグイン

その他のプラグインを探す場合は、Apache Cordova PLUGINS REGISTRYで探します。
「Apache Cordova」が提供しているプラグイン以外のプラグインもありますので、プラグインを独自に作成する前に一度探してみたほうがいいでしょう。

参考

Cordova Plugin Device

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12