添加CordovaLib
在cordova的安裝目錄下可以找到CordovaLib庫,如果找不到的話固逗,可以通過新建一個demo cordova工程文件并添加ios平臺然后在該demo工程中找到該CordovaLib庫区拳。
將CordovaLib拷貝到Xcode中拘领,然后
- 將工程中的File Inspector面板中的Relative to Group改為Location
- 在Project Navigator面板中的** Target的Build Settings**添加
-force_load -ObjC
- 點擊Build Phases 中的Link Binaries with Libraries的+,添加下列frameworks
AssetsLibrary.framework
CoreLocation.framework
CoreGraphics.framework
MobileCoreServices.framework
- 在Target Dependencies面板中添加CordovaLib
- 再在Link Binaries with Libraries面板中添加libCordova.a
- 將Xcode Preferences → Locations → Derived Data → Advanced設(shè)置成Unique
- 最后在Target面板中找到Build Settings然后在Header Search Paths.添加下面的值
"$(TARGET_BUILD_DIR)/usr/local/lib/include"
"$(OBJROOT)/UninstalledProducts/include"
"$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include"
"$(BUILT_PRODUCTS_DIR)"
到這里就ok了樱调,累死我了
添加h5資源
將網(wǎng)頁資源放置到工程文件夾中约素,所有網(wǎng)頁資源的根目錄為www文件夾
配置cofig.xml文件
在工程中放置一個config.xml文件,基本信息如下
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.jahome.ezhan.merchant" version="1.1.0.4" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<feature name="View">//feature標簽為插件信息笆凌,擴展新插件時要在這邊配置信息
<param name="ios-package" value="CDVView" />
</feature>
<feature name="Pay">
<param name="ios-package" value="CDVPay" />
</feature>
<feature name="Image">
<param name="ios-package" value="CDVImage" />
</feature>
<name>
家和e站·商家
</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="wuhuarong@star-net.cn" href="">
Wei-Ju Team
</author>
<content src="views/index.html" />//web入口頁
<access origin="*" />//下面是一些配置參數(shù)圣猎,比如可跳轉(zhuǎn)的鏈接,后臺音頻播放什么的
<allow-navigation href="*" />
<allow-intent href="*" />
<allow-navigation href="http://*/*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel://*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
<preference name="AllowInlineMediaPlayback" value="false" />
<preference name="BackupWebStorage" value="local" />
<preference name="DisallowOverscroll" value="true" />
<preference name="EnableViewportScale" value="false" />
<preference name="KeyboardDisplayRequiresUserAction" value="true" />
<preference name="MediaPlaybackRequiresUserAction" value="false" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="SuppressesLongPressGesture" value="false" />
<preference name="Suppresses3DTouchGesture" value="false" />
<preference name="GapBetweenPages" value="0" />
<preference name="PageLength" value="0" />
<preference name="PaginationBreakingMode" value="page" />
<preference name="PaginationMode" value="unpaginated" />
<preference name="StatusBarOverlaysWebView" value="true" />
<preference name="StatusBarStyle" value="lightcontent" />
</widget>
cordova使用
先import Cordova框架
#import <Foundation/Foundation.h>
然后可以通過下面的代碼獲取CDVViewController
+(CDVViewController *)getCDVViewController{
if(CDV_VIEW_CONTROLLER==nil)
CDV_VIEW_CONTROLLER = [[CDVViewController alloc]init];
return CDV_VIEW_CONTROLLER;
}
接著就可以在相應(yīng)的視圖樹中添加
_viewController.view.frame = self.view.bounds;
[self.view addSubview:_viewController.view];
js調(diào)用插件
我們在重新看一下cofig.xml中插件的描述信息,比如支付插件
<feature name="Pay">//Pay為插件名稱乞而,在js需要傳入對應(yīng)名稱才能調(diào)用相應(yīng)插件
<param name="ios-package" value="CDVPay" />//對應(yīng)的源文件名稱
</feature>
然后看一下我們的插件實現(xiàn)的源文件
@interface CDVPay :CDVPlugin //需要繼承CDVPlugin
- (void)pay:(CDVInvokedUrlCommand*)command;//該插件提供給js的功能
@end
@implementation CDVPay
- (void)pay:(CDVInvokedUrlCommand *)command{//功能實現(xiàn)
NSString* charge = [command.arguments objectAtIndex:0];
[Pingpp createPayment:charge
viewController:self.viewController
appURLScheme:kUrlScheme
withCompletion:^(NSString *result, PingppError *error) {
NSLog(@"CDVPay--result: %@", result);
NSDictionary* dic = @{@"result":result};
NSError* jsonError;
NSData* josnData = [NSJSONSerialization dataWithJSONObject:dic options:nil error:&jsonError];
[CDVUtils excJSAction:ACTION_ON_PAY_RESULT args:[[NSString alloc] initWithData:josnData encoding:NSUTF8StringEncoding]];
}];
}
@end
再然后送悔,我們在js中,通過cordova.js中的cordova對象的exec方法來調(diào)用原生插件的方法
//Pay插件名稱,pay為action名稱爪模, [chargeInfo]為參數(shù)數(shù)組
cordova.exec(null, null, "Pay", "pay", [chargeInfo])
在插件源文件中欠啤,我們通過command.arguments來獲取js傳遞過來的參數(shù)
NSString* charge = [command.arguments objectAtIndex:0];
如果我們需要返回一些結(jié)果給js的話,可以通過commandDelegate對象
CDVPluginResult* pluginResult = nil;
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"message"];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
在剛才的js代碼中注冊回調(diào)即可收到原生返回來的消息了
function success(re){//成功回調(diào)
}
function fail(re){//失敗回調(diào)
}
cordova.exec(success, fail, "Pay", "pay", [chargeInfo])
原生調(diào)用js方法
有時候需要原生主動調(diào)用js的方法屋灌,可以通過CommandDelegate的evalJs方法
NSString* js = [NSString stringWithFormat:@"excJSAction('%@','%@')",action,args];
if(CDV_VIEW_CONTROLLER!=nil)
[CDV_VIEW_CONTROLLER.commandDelegate evalJs:js];
然后我們在web頁面中洁段,必須申明有要調(diào)用的方法
var excJSAction = function(action, args) {
}