cordova集成说明--ios

添加CordovaLib

在cordova的安装目录下可以找到CordovaLib库,如果找不到的话,可以通过新建一个demo cordova工程文件并添加ios平台然后在该demo工程中找到该CordovaLib库。
将CordovaLib拷贝到Xcode中,然后

  1. 将工程中的File Inspector面板中的Relative to Group改为Location
  2. Project Navigator面板中的** TargetBuild Settings**添加
-force_load -ObjC
  1. 点击Build Phases 中的Link Binaries with Libraries的+,添加下列frameworks
AssetsLibrary.framework
CoreLocation.framework
CoreGraphics.framework
MobileCoreServices.framework
  1. Target Dependencies面板中添加CordovaLib
  2. 再在Link Binaries with Libraries面板中添加libCordova.a
  3. 将Xcode Preferences → Locations → Derived Data → Advanced设置成Unique
  4. 最后在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资源

将网页资源放置到工程文件夹中,所有网页资源的根目录为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="*" />//下面是一些配置参数,比如可跳转的链接,后台音频播放什么的
    <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;
}

接着就可以在相应的视图树中添加

_viewController.view.frame = self.view.bounds;
[self.view addSubview:_viewController.view];

js调用插件

我们在重新看一下cofig.xml中插件的描述信息,比如支付插件

<feature name="Pay">//Pay为插件名称,在js需要传入对应名称才能调用相应插件
        <param name="ios-package" value="CDVPay" />//对应的源文件名称
</feature>

然后看一下我们的插件实现的源文件

@interface CDVPay :CDVPlugin  //需要继承CDVPlugin
- (void)pay:(CDVInvokedUrlCommand*)command;//该插件提供给js的功能
@end

@implementation CDVPay
    
- (void)pay:(CDVInvokedUrlCommand *)command{//功能实现
    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方法来调用原生插件的方法

//Pay插件名称,pay为action名称, [chargeInfo]为参数数组
cordova.exec(null, null, "Pay", "pay", [chargeInfo])

在插件源文件中,我们通过command.arguments来获取js传递过来的参数

NSString* charge = [command.arguments objectAtIndex:0];

如果我们需要返回一些结果给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代码中注册回调即可收到原生返回来的消息了

function success(re){//成功回调
}
function fail(re){//失败回调
}
cordova.exec(success, fail, "Pay", "pay", [chargeInfo])

原生调用js方法

有时候需要原生主动调用js的方法,可以通过CommandDelegate的evalJs方法

NSString* js = [NSString stringWithFormat:@"excJSAction('%@','%@')",action,args];
if(CDV_VIEW_CONTROLLER!=nil)
    [CDV_VIEW_CONTROLLER.commandDelegate evalJs:js];

然后我们在web页面中,必须申明有要调用的方法

var excJSAction = function(action, args) {
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容