cordova / phonegap 3.0 device properties

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

I am new to phonegap / cordova 3.0 and it seems that I have a similar problem as PhoneGap Help: device properties, cordova v phonegap, xcode debugging. Unfortunately I couldn't find a solution in the internet by now.

After having created the Hello World example for ios everything works fine in the simulator. But after having changed the content in the index.html with the code of the example given for device properties in den documentation the screen of the simulator shows only "Loading device properties.." Nothing more. For some reason the function onDeviceReady() does not work properly. Any help is highly appreciated

Here is the code of index.html for the device properties

<!DOCTYPE html> 

Device Properties Example

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>     <script type="text/javascript" charset="utf-8">          // Wait for device API libraries to load         //         document.addEventListener("deviceready", onDeviceReady, false);          // device APIs are available         //         function onDeviceReady() {             var element = document.getElementById('deviceProperties');             element.innerHTML = 'Device Name: '     + device.name     + '<br />' +             'Device Cordova: '  + device.cordova  + '<br />' +             'Device Platform: ' + device.platform + '<br />' +             'Device UUID: '     + device.uuid     + '<br />' +             'Device Model: '    + device.model    + '<br />' +             'Device Version: '  + device.version  + '<br />';         }          </script> </head> <body>     <p id="deviceProperties">Loading device properties...</p> </body> 

回答1:

As I mentioned in the other question you linked to, I think the issue here may be with a missing plugin. Your onDeviceReady function should be working (try throwing an alert in there, just to test it out)... but I think you may be running into issues when you're trying to access things like device.model and device.version.

In order to access the device object, you need to install the plugin. Copying in the example code won't work without the plugin. If you haven't done so already, grab the plugin using:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git 

Hopefully that does the trick for you.



回答2:

For a Newbie like me is to mention that the plugin link must be used in the project directory and the .h and .m files of the plugin must be moved into the project plugins directory. Not to forget that the DeviceDetails.js must go into the same directory as the index.js



回答3:

In addition to avoisions answer and to avoid path issues like Helmut encountered:

  1. Open Terminal/Shell
  2. Go to directory of your Phonegap project ($ cd path/to/project)
  3. Type in the following into the Terminal/Shell:

    cordova plugin add org.apache.cordova.device

Unfortunately one can't distinguish between different devices of the same platform with this method. For example if you'd have to determine if the app is viewed on an iPhone 5 you should probably retrieve the screen size.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!