Cordova官方文檔整理

一二拐、install Cordova

  1. install Node.js
  2. install cordova:
    $ sudo npm install -g cordova

二芭碍、Create the App

  1. create:$ cordova create hello(directory) com.example.hello HelloWorld(projectName)
  2. add platforms:$ cd hello
    $ cordova platform add ios
  3. check platforms:$ cordova platforms ls
  4. remove platforms:$ cordova platform remove/rm blackberry10

三折联、run the App

  1. build:$ cordova build or $ cordova build/prepare/compile ios
  2. test:$ cordova emulate ios

四、add plugins

  1. search:$ cordova plugin search ***
  2. add:$ cordova plugin add cordova-plugin-device
  3. check:cordova plugin ls

advanced

1. version:`$ cordova plugin add cordova-plugin-console@latest(0.2.1)`
2. URL:`$ cordova plugin add https://github.com/apache/cordova-plugin-console.git`
3. tag or branch:`$ cordova plugin add https://github.com/apache/cordova-plugin-console.git#r0.2.0`
4. subdirectory:`$ cordova plugin add https://github.com/someone/aplugin.git#:/my/sub/dir`
5. local path:`$ cordova plugin add ../my_plugin_dir`

五、updating Cordova and project

  1. updating Cordova:$ sudo npm update -g cordova or $ sudo npm install -g cordova@3.1.0-0.2.0
  2. listing the info:$ npm info cordova
  3. updating the project:$ cordova platform update ios

六仿贬、Adding Cleaver to the Xcode Project (CordovaLib Sub-Project)

  1. Quit Xcode if it is running.

  2. Open a terminal and navigate to the source directory for Cordova iOS.

  3. Copy the config.xml file described above into the project directory.

  4. Open Xcode and use the Finder to copy the config.xml file into its Project Navigator window.

  5. Choose Create groups for any added folders and press Finish.

  6. Use the Finder to copy the CordovaLib/CordovaLib.xcodeproj file into Xcode's Project Navigator

  7. Select CordovaLib.xcodeproj within the Project Navigator.

  8. Type the Option-Command-1 key combination to show the File Inspector.

  9. Choose Relative to Group in the File Inspector for the drop-down menu for Location.

  10. Select the project icon in the Project Navigator, select the Target, then select the Build Settings tab.

  11. Add -force_load and -Obj-C for the Other Linker Flags value.

  12. Click on the project icon in the Project Navigator, select the Target, then select the Build Phases tab.

  13. Expand Link Binaries with Libraries.

  14. Select the + button, and add the following frameworks. Optionally within the Project Navigator, move them under the Frameworks group:

AssetsLibrary.framework 
CoreLocation.framework 
CoreGraphics.framework 
MobileCoreServices.framework
  1. Expand Target Dependencies, the top box with that label if there's more than one box.

  2. Select the + button, and add the CordovaLib build product.

  3. Expand Link Binaries with Libraries, the top box with that label if there's more than one box.

  4. Select the + button, and add libCordova.a.

  5. Set the Xcode Preferences → Locations → Derived Data → Advanced... to Unique.

  6. Select the project icon in the Project Navigator, select your Target, then select the Build Settings tab.

  7. Search for Header Search Paths. For that setting, add these three values below, including the quotes:

 "$(TARGET_BUILD_DIR)/usr/local/lib/include"        
 "$(OBJROOT)/UninstalledProducts/include"
 "$(BUILT_PRODUCTS_DIR)"

七、using CDVViewController

two properties:wwwFolderName and startPage

八墓贿、about Plugin

An iOS plugin is implemented as an Objective-C class that extends the CDVPlugin class. For JavaScript's exec method's service parameter to map to an Objective-C class, each plugin class must be registered as a < feature> tag in the named application directory's config.xml file.

  1. exec:cordova.exec
    exec(<successFunction>, <failFunction>, <service>, <action>, [<args>]);

This marshals a request from the UIWebView to the iOS native side, effectively calling the action method on the service class, with the arguments passed in the args array.

  1. plugin method:
        - (void)myMethod:(CDVInvokedUrlCommand*)command
        {
            CDVPluginResult* pluginResult = nil;
            NSString* myarg = [command.arguments objectAtIndex:0];
            if (myarg != nil) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
            } else {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
            }
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
  1. The JavaScript Interface

    note:The JavaScript provides the front-facing interface, making it perhaps the most important part of the plugin. You can structure your plugin's JavaScript however you like, but you need to call cordova.exec to communicate with the native platform, using the following syntax:

    cordova.exec(function(winParam) {},
                 function(error) {},
                 "service",
                 "action",
                 ["firstArgument", "secondArgument", 42, false]);

parameters:

  • function(winParam) {}: A success callback function. Assuming your exec call completes successfully, this function executes along with any parameters you pass to it.

  • function(error) {}: An error callback function. If the operation does not complete successfully, this function executes with an optional error parameter.

  • "service": The service name to call on the native side. This corresponds to a native class, for which more information is available in the native guides listed below.

  • "action": The action name to call on the native side. This generally corresponds to the native class method. See the native guides listed below.

  • [/* arguments */]: An array of arguments to pass into the native environment.

sample Java Script

js interface:

     window.echo = function(str, callback) {
        cordova.exec(callback, function(err) {
            callback('Nothing to echo.');
        }, "Echo", "echo", [str]);
    };

九茧泪、config.xml

  1. Global Preferences
    Fullscreen allows you to hide the status bar, default is false.

  2. Multi-Platform Preferences

  • DisallowOverscroll set to true to forbid the interface to display any feedback
  • HideKeyboardFormAccessoryBar defaults to false,set to true to hide the additional toolbar
  • Orientation to lock orientation and prevent the interface from rotating in response to changed in orientation.

十、Events

  1. deviceready:when Cordova fully loaded.
    document.addEventListener("deviceready", yourCallbackFunction, false);

    note:
    The deviceready event fires once Cordova has fully loaded. Once the event fires, you can safely make calls to Cordova APIs. Applications typically attach an event listener with document.addEventListener once the HTML document's DOM has loaded.

    The deviceready event behaves somewhat differently from others. Any event handler registered after the deviceready event fires has its callback function called immediately.

    example

    <!DOCTYPE html>
    <html>
      <head>
        <title>Device Ready Example</title>
    
        <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
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }
    
        // device APIs are available
        //
        function onDeviceReady() {
            // Now safe to use device APIs
        }
    
        </script>
      </head>
      <body onload="onLoad()">
      </body>
    </html>
  1. pause:fires when application is put into the background
    document.addEventListener("pause", yourCallbackFunction, false);

    example:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Pause Example</title>
        <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
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }
        // device APIs are available
        //
        function onDeviceReady() {
            document.addEventListener("pause", onPause, false);
        }
    
        // Handle the pause event
        //
        function onPause() {
        }
    
        </script>
      </head>
      <body onload="onLoad()">
      </body>
    </html>

note:
In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). They are only processed when the app resumes, on the next run loop.

  1. resume:when an application is retrieved from the background
    document.addEventListener("resume", yourCallbackFunction, false);

    example:

 <!DOCTYPE html>
    <html>
      <head>
        <title>Resume Example</title>
    
        <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
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }
    
        // device APIs are available
        //
        function onDeviceReady() {
            document.addEventListener("resume", onResume, false);
        }
    
        // Handle the resume event
        //
        function onResume() {
        }
    
        </script>
      </head>
      <body onload="onLoad()">
      </body>
    </html>

note:
Any interactive functions called from a pause event handler execute later when the app resumes, as signaled by the resume event. These include alerts, console.log(), and any calls from plugins or the Cordova API, which go through Objective-C.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末聋袋,一起剝皮案震驚了整個濱河市队伟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌幽勒,老刑警劉巖嗜侮,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異啥容,居然都是意外死亡锈颗,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進(jìn)店門干毅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宜猜,“玉大人,你說我怎么就攤上這事硝逢∫逃担” “怎么了绅喉?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長叫乌。 經(jīng)常有香客問我柴罐,道長,這世上最難降的妖魔是什么憨奸? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任革屠,我火速辦了婚禮,結(jié)果婚禮上排宰,老公的妹妹穿的比我還像新娘似芝。我一直安慰自己,他們只是感情好板甘,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布党瓮。 她就那樣靜靜地躺著,像睡著了一般盐类。 火紅的嫁衣襯著肌膚如雪寞奸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天在跳,我揣著相機(jī)與錄音枪萄,去河邊找鬼。 笑死猫妙,一個胖子當(dāng)著我的面吹牛瓷翻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吐咳,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼逻悠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了韭脊?” 一聲冷哼從身側(cè)響起童谒,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎沪羔,沒想到半個月后饥伊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蔫饰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年琅豆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片篓吁。...
    茶點(diǎn)故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡茫因,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出杖剪,到底是詐尸還是另有隱情冻押,我是刑警寧澤驰贷,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站洛巢,受9級特大地震影響括袒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜稿茉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一锹锰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧漓库,春花似錦恃慧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽砰琢。三九已至蘸嘶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間陪汽,已是汗流浹背训唱。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挚冤,地道東北人况增。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像训挡,于是被迫代替她去往敵國和親澳骤。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容