cordovar,ionic与原生应用混编iOS

首先生成一个新的cordovar项目,
cordova create hello com.example.hello HelloWorld 

添加平台
cordova platform add android

cordova platform add ios

完成后运行以下命令查看:

cordova platfrom list

移除Android平台支持

cordova platform rm android

https://ionicframework.com/getting-started/
https://ionicframework.com/docs/v2/intro/deploying/

安装node
npm install -g cordova ionic //添加ionic
cd 到项目目录
mkdir www
npm install
cordova platform add android ios
ionic serve //浏览器预览项目
ionic run android
兼容android 4.4以前版本:cordova plugin add cordova-plugin-crosswalk-webview
http请求参考 http://blog.ionic.io/10-minutes-with-ionic-2-calling-an-api/

由于ios工程使用了cocoa pods,所以添加cordova-plugin-cocoapod-support插件,添加库文件:https://github.com/blakgeek/cordova-plugin-cocoapods-support

cordova plugin add cordova-plugin-cocoapod-support --save

在comfit.xml里面添加配置库,代码如下:

<platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <preference name="pods_ios_min_version" value="9.0" />
        <preference name="pods_use_frameworks" value="true" />
        <pod branch="4.2.0" git="https://github.com/Alamofire/Alamofire" name="Alamofire" />
    platform>

这里执行ionic build iOS 时候,会自动生成cocoa pods的库文件,
但是由于cocoa pods版本及oc与swift的问题,需要自己在生成工程后,重新使用自己的cocoa pods,更改pod file文件,执行pod install,这样生成的工程才会对应版本,正常编译。

这里工程如果是有swift,需要把头文件Bridging-Header.h**路径写对**,否则报错
这里写图片描述

cordovar生成的iOS工程是oc的,MainViewController.h就是首页,
这里appdelete里面可以添加自己的配置,比如键盘IQKeyboardManager:

#import "AppDelegate.h"
#import "MainViewController.h"
#import "IQKeyboardManager/IQKeyboardManager.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;

    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

当然按着正常的ios工程代码走就可以,我还更改了父类CDVAppDelegate.h这里,以便导航栏的显示

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    self.window = [[UIWindow alloc] initWithFrame:screenBounds];
    self.window.autoresizesSubviews = YES;

    // only set if not already set in subclass
    if (self.viewController == nil) {
        self.viewController = [[CDVViewController alloc] init];
    }
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];//先将root添加在navigation上
    [nav setNavigationBarHidden:NO animated:YES];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    self.window.rootViewController = nav;
//    [_window setRootViewController:nav];//navigation加在window上

    [self.window makeKeyAndVisible];

    return YES;
}

写插件,调用原生代码

我用的swift3.0写的

import Foundation
import HandyJSON
@objc(EnnSjCDVPlugin) class EnnSj : CDVPlugin {

    //验证口令方法
    func coolMethod(_ command:CDVInvokedUrlCommand)
    {
        //返回结果
        var pluginResult:CDVPluginResult?
        //获取参数
        let password = command.arguments[0] as? String
        //开始验证
        if password == nil || password == "" {
            pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR,
                                           messageAs: "口令不能为空")
        }else if password != "hangge" {
            pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR,
                                           messageAs: "口令不正确")
        }else{
            pluginResult = CDVPluginResult(status:CDVCommandStatus_OK)
        }
        //发送结果
        self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
    }

    func pushViewController(_ viewController: UIViewController, animated: Bool) {
self.viewController.navigationController?.navigationBar.isHidden = false
self.viewController.navigationController?.pushViewController(viewController, animated: animated)
    }

//插件调用接口需要在self.commandDelegate.run {}线程里面实现
}

h5页面与本地广播插件

https://github.com/bsorrentino/cordova-broadcaster/blob/master/README.md

添加插件:cordova plugin add cordova-plugin-broadcaster

用法与原生的用法一样的

Swift 3.0

let nc = NSNotificationCenter.default
nc.post(name:"didShow", object: nil, userInfo: ["data":"test"])

我用到的插件:

<plugin name="ionic-plugin-keyboard" spec="~2.2.1" />
    <plugin name="cordova-plugin-whitelist" spec="1.3.1" />
    <plugin name="cordova-plugin-console" spec="1.0.5" />
    <plugin name="cordova-plugin-statusbar" spec="2.2.1" />
    <plugin name="cordova-plugin-device" spec="1.1.4" />
    <plugin name="cordova-plugin-splashscreen" spec="~4.0.1" />
    <plugin name="cordova-plugin-broadcaster" spec="~2.1.0" />
    <plugin name="cordova-plugin-cocoapod-support" spec="~1.3.0" />
    <plugin name="com.ewwon.pmnns.enn.sj" spec="ssh://[email protected]:13022/ewwcloud/cordova-plugin-enn-sj.git" />

你可能感兴趣的:(Cordova框架)