Cordova 快速入门记录
记一笔
Cordova 官网入门文档 Get Started Fast,言简意该、通俗易懂。
在此仅记录过程及安装时遇到的问题,做个纪念什么的也好。
开始使用
完全就是根据官方文档,敲敲命令而已
安装 Cordova
xinpureZhu:cordova xinpure$ sudo npm install -g cordova
创建项目
xinpureZhu:cordova xinpure$ cordova create MyApp
Creating a new cordova project.
添加平台支持
Cordova 现支持的平台 amazon-fireos
, android
, blackberry10
, browser
, firefoxos
, ios
, osx
, webos
这里便随着文档,选择了 browser
xinpureZhu:cordova xinpure$ cd MyApp/
xinpureZhu:MyApp xinpure$ ls
config.xml hooks platforms plugins www
xinpureZhu:MyApp xinpure$ cordova platform
Installed platforms:
Available platforms: amazon-fireos, android, blackberry10, browser, firefoxos, ios, osx, webos
xinpureZhu:MyApp xinpure$ cordova platform add browser
Adding browser project...
Creating Browser project. Path: platforms/browser
Discovered plugin "cordova-plugin-whitelist" in config.xml. Installing to the project
Fetching plugin "cordova-plugin-whitelist@1" via npm
Installing "cordova-plugin-whitelist" for browser
启动 App
xinpureZhu:MyApp xinpure$ cordova run browser
Running command: /Project/cordova/MyApp/platforms/browser/cordova/run
Static file server running on port 8000 (i.e. http://localhost:8000)
CTRL + C to shut down
Static file server running @ http://localhost:8000/index.html
CTRL + C to shut down
Executing command: open -n -a "Google Chrome" --args --user-data-dir=/tmp/temp_chrome_user_data_dir_for_cordova http://localhost:8000/index.html
到此,Cordova 快速入门便结束了,确实比较简单的
为项目添加 ios 平台支持
WebApp 当然还是要在手机上跑起来才到位,这里使用 ios
尝尝鲜
为项目添加ios平台
xinpureZhu:MyApp xinpure$ cordova platform add ios
Adding ios project...
iOS project created with cordova-ios@3.9.1
Installing "cordova-plugin-whitelist" for ios
以新平台启动App
xinpureZhu:MyApp xinpure$ cordova run ios
Running command: /Project/cordova/MyApp/platforms/ios/cordova/run
ios-sim was not found. Please download, build and install version 3.0.0 or greater from https://github.com/phonegap/ios-sim into your path, or do 'npm install -g ios-sim'
ERROR running one or more of the platforms: Error: /Project/cordova/MyApp/platforms/ios/cordova/run: Command failed with exit code 2
You may not have the required environment or OS to run this project
执行命令终于不是那么顺畅了,缺少 ios-sim
模块 (解决方法已清楚的提示,所以也不算是问题啦)
按提示执行命令:
xinpureZhu:MyApp xinpure$ sudo npm install -g ios-sim
安装完成后再重新执行启动命令:
xinpureZhu:MyApp xinpure$ cordova run ios
Running command: /Project/cordova/MyApp/platforms/ios/cordova/run
simctl was not found.
Check that you have Xcode 6.x installed:
xcodebuild --versionCheck that you have Xcode 6.x selected:
xcode-select --print-path
ERROR running one or more of the platforms: Error: /Project/cordova/MyApp/platforms/ios/cordova/run: Command failed with exit code 2
You may not have the required environment or OS to run this project
好吧,看到这个错误提示,我的内心是茫然的。
我使用的是 Mac OS X EI Capitan, Xcode 也更新到了 7.0.1, 何故如此?
再次 查看文档后发现不同于 bowser
平台,ios
平台需要先 build
其实之前也看到过 build
这一操作,但是思维定势,想当然的以为 cordova run
会同时执行 cordova build
,然而并不是。
Build App
xinpureZhu:MyApp xinpure$ cordova platform ls
Installed platforms: browser 4.0.0, ios 3.9.1
Available platforms: amazon-fireos, android, blackberry10, firefoxos, osx, webos
xinpureZhu:MyApp xinpure$ cordova build ios
...此处省略一连串 Build 信息..
** BUILD SUCCEEDED **
出现了 SUCCEEDED
显然已经成功了
使用仿真器启动 App
xinpureZhu:MyApp xinpure$ cordova emulate ios
此处依然省略大量, 启动时的编译信息。启动成功也会 SUCCEEDED
提示,同时 Xcode 仿真器也会启动并打开 App
总结
虽然官方已经提供了详细的说明文档,文档也简单易懂,但是不管多简单还是要亲自动手敲敲命令,说不定就会出现一些意想不到的问题。