前言
現在做項目為了節約成功,適配多平臺 cordova不為是一個很好的選擇。個人覺得以后也許是一個趨勢!像一些知名的APP 比如支付寶 淘寶 好多都大量集成了HTML5 頁面!像我們公司現在的APP 大多都是h5頁面 感覺原生都沒什么事情做了。可想而知學習cordova的重要性!
簡介
Cordova提供了一組設備相關的API,通過這組API,移動應用能夠以JavaScript訪問原生的設備功能,如攝像頭、麥克風等。Cordova還提供了一組統一的JavaScript類庫,以及為這些類庫所用的設備相關的原生后臺代碼。Cordova支持如下移動操作系統:iOS, Android,ubuntu phone os, Blackberry, Windows Phone, Palm WebOS, Bada 和 Symbian。
入門第一步:下載資源庫并集成到項目中來
去下載cordova 庫的源碼 可以去github上下載 也可以去官網下載。其實官網上的代碼也是放到github上的!
官方網站:http://cordova.apache.org/
github資源下載地址:https://github.com/apache/cordova-ios
本地下載地址:http://xiazai.jb51.net/201711/yuanma/cordova-ios(jb51.net).rar
使用 CocoaPods進行第三方庫的管理 我之前用的是cordova3.8.0 前幾天不久更新到了4.0.1 。 4.0.1 只支持8.0以上 以下的你用CocoaPods 更新會提示錯誤!注意一下就可以了!
platform :ios, '8.0'
pod 'Cordova', '~> 4.0.1'
入門第二步:如何在項目中正確的集成 cordova4.0.1 庫
1. 配置 Config.xml 很重要
Config.xml is a global configuration file that controls many aspects of a cordova application's behavior. This platform-agnostic XML file is arranged based on the W3C'sPackaged Web Apps (Widgets)specification, and extended to specify core Cordova API features, plugins, and platform-specific settings.For projects created with the Cordova CLI (described in The Command-Line Interface), this file can be found in the top-level directory:
大致意思就是: config.xml 是一個全局配置文件,控制一個cordova應用行為的許多方面。這種平臺無關的XML文件是基于w3c'spackaged Web應用程序設置(widgets)規范,并擴展到指定核心cordovaAPI功能,插件和平臺的具體設置。與科爾多瓦CLI創建的項目(描述在命令行界面)
http://cordova.apache.org/docs/en/6.x/config_ref/index.html 官方詳解
2. 顯示html5頁面需要一個容器 在這里使用CDVViewController類 進行html5的顯示以及控制 當然也可以自定義一個容器繼承CDVViewController類做一些定制功能。
如何使用如下:
self.cordovaManageVC= [[CordovaManageVCalloc]init]; self.cordovaManageVC.startPage=@"http://www.baidu.com"; [self.navigationControllerpushViewController:self.cordovaManageVCanimated:YES];
3. 設置用戶代理
- (instancetype)init { self= [superinit]; if(self!=nil) { //設置用戶代理 如不設置無法調用與H5定制的JavaScript方法進行交互 NSString* original = [CDVUserAgentUtiloriginalUserAgent]; NSString* userAgent = [originalstringByAppendingString:@"delegateUserName"]; self.baseUserAgent= userAgent; } returnself; }
4. 自定義插件 需要使用到 cordova的CDVPlugin類 用子類來集成CDVPlugin
/** *返回控件 * *@param command */ - (void)back:(CDVInvokedUrlCommand*)command;
定制完成之后需要在confing.xml 進行配置
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com