【PhoneGap】环境安装配置浅析
由于项目需要,需要了解PhoneGap架构以其在各个平台的环境配置,阅读了官网部分文档,总结如下。
【官网地址】
http://phonegap.com/
【环境配置】
参考: http://phonegap.com/install/
1. 安装NodeJs。 打开NodeJs的官网:http://nodejs.org/ 点击“install”,即可完成安装。
NodeJS安装完成后,会自带把npm安装好,npm也在第二步中需要用到。
2. 安装phoneGap
在控制台运行命令:
sudo npm install -g phonegap
输出结果如下:
- /usr/local/bin/phonegap -> /usr/local/lib/node_modules/phonegap/bin/phonegap.js
- phonegap@3.1.0-0.15.0 /usr/local/lib/node_modules/phonegap
- ├── colors@0.6.0-1
- ├── pluralize@0.0.4
- ├── semver@1.1.0
- ├── qrcode-terminal@0.9.4
- ├── shelljs@0.1.4
- ├── optimist@0.6.0 (wordwrap@0.0.2, minimist@0.0.5)
- ├── node-static@0.7.0 (colors@0.6.2, mime@1.2.11)
- ├── phonegap-build@0.8.4 (qrcode-terminal@0.8.0, optimist@0.3.7, shelljs@0.0.9, phonegap-build-api@0.3.3)
- ├── prompt@0.2.11 (revalidator@0.1.5, pkginfo@0.3.0, read@1.0.5, winston@0.6.2, utile@0.2.0)
- └── cordova@3.1.0-0.1.0 (ncallbacks@1.0.0, colors@0.6.2, open@0.0.3, mime@1.2.11, shelljs@0.1.2, follow-redirects@0.0.3, tar@0.1.18, prompt@0.2.7, glob@3.2.6, elementtree@0.1.5, xcode@0.5.1, npm@1.3.13, express@3.0.0, request@2.22.0, plist@0.4.3, ripple-emulator@0.9.18, plugman@0.13.0)
3. 使用phoneGap
输入命令:
- $ phonegap create my-app
- $ cd my-app
- $ phonegap run android
其中,可以关联create命令: (在bashrc中关联bin里面的create命令)
- # PhoneGap
- alias pgap='/Library/PhoneGap/bin/create'
‘=’右边为具体的phoneGap路径。
创建工程的格式为:
- pgap <project_folder_path> <package_name> <project_name>
【PhoneGap在IOS平台的应用】
官网链接为: http://docs.phonegap.com/en/edge/guide_platforms_ios_index.md.html#iOS%20Platform%20Guide
- $ cordova create hello com.example.hello "HelloWorld"
- $ cd hello
- $ cordova platform add ios
- $ cordova prepare # or "cordova build"
[error1] cordova: command not found
[solution1]
需要安装:
sudo npm install -g cordova
cordova 和phonegap虽然是来自同一家,但还是不同的,他们的关系就像webkit和google chrome。。要运行这个cordova当然得安装。
[solution2]
执行命令及结果:
- Scotts-iMac:helloworld mac$ phonegap create hello com.example.hello "HelloWorld"
- [phonegap] missing library phonegap/www/3.1.0
- [phonegap] downloading https://github.com/phonegap/phonegap-app-hello-world/archive/3.1.0.tar.gz...
- [phonegap] created project at /Users/mac/Documents/PhoneGap/helloworld/hello
[error2] 运行命令:phonegap run ios 失败
[solution2]
- phonegap run ios
- [phonegap] detecting iOS SDK environment...
- [phonegap] using the local environment
- [phonegap] adding the iOS platform...
- [phonegap] missing library cordova/ios/3.1.0
- [phonegap] downloading https://git-wip-us.apache.org/repos/asf?p=cordova-ios.git;a=snapshot;h=3.1.0;sf=tgz...
- [phonegap] compiling iOS...
- [phonegap] successfully compiled iOS app
- [phonegap] trying to install app onto device
- [phonegap] no device was found
- [phonegap] trying to install app onto emulator
- [warning] missing ios-sim
- [warning] install ios-sim from http://github.com/phonegap/ios-sim
- [error] An error occurred while emulating/deploying the ios project.=== CLEAN TARGET CordovaLib OF PROJECT CordovaLib WITH CONFIGURATION Debug ===
但这个命令会再对应路径下生成响应的IOS 工程文件,打开“HelloWorld.xcodeproj”, 可以正常编译,运行。
错误的原因是签名导致,没有正确的profile文件。
【PhoneGap系统架构】
网上找的架构图如下:
【IOS中使用方法】
- #import <Cordova/CDVViewController.h>
- CDVViewController* viewController = [CDVViewController new];
- viewController.wwwFolderName = @"myfolder";
- viewController.startPage = @"mystartpage.html";
- viewController.useSplashScreen = YES;
- viewController.view.frame = CGRectMake(0, 0, 320, 480);
- [myView addSubview:viewController.view];