iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么在IOS中使用Cordova插件
  • 280
分享到

怎么在IOS中使用Cordova插件

2023-06-14 12:06:26 280人浏览 独家记忆
摘要

这篇文章给大家分享的是有关怎么在iOS中使用Cordova插件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、准备插件功能:打开ioS相机1:创建插件plugman create --name [插件名称] -

这篇文章给大家分享的是有关怎么在iOS中使用Cordova插件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、准备

插件功能:打开ioS相机

1:创建插件

plugman create --name [插件名称] --plugin_id [插件ID] --plugin_version [插件版本号]
plugman create --name CameraDemo --plugin_id cordova-plugin-camerademo --plugin_version 1.0.0

2:添加IOS平台

plugman platfORM add --platform_name ios

3:创建package.JSON文件

以下两种都可以生成package.json
1:使用命令 “npm init” 创建package.json文件
2:plugman createpackagejson [插件路径]
原应用使用的ionic UI框架,没有package.json无法安装插件

最终插件目录结构

怎么在IOS中使用Cordova插件

除了ViewController.h和ViewController.m文件,其余的文件通过上述步骤都会自动生成

二、过程

创建文件ViewController.h和ViewController.m
ViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{}@property (nonatomic,strong) UIImagePickerController *imagePicker;- (void)getDeviceInfo;  //获取ios设备信息- (void)OpenCamera;//打开ios相机@end

ViewController.m

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (id) init{    NSLog(@"=======================相机初始化");    self = [super init];    self.imagePicker = [[UIImagePickerController alloc] init];    return self;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.        UIButton *button =[[UIButton alloc]init];    [button setTitle:@"我是按钮" forState:(UIControlStateNormal)];    [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];    [button setTitleColor:[UIColor blueColor] forState:(UIControlStateHighlighted)];    [button setBackgroundColor:[UIColor yellowColor]];    [button setFrame:CGRectMake(10, 50, 100, 30)];    //事件    //[button addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];    [self.view addSubview:button];        UIButton *deviceBtn =[[UIButton alloc]init];    [deviceBtn setTitle:@"查看设备信息" forState:(UIControlStateNormal)];    [deviceBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];    [deviceBtn setTitleColor:[UIColor blueColor] forState:(UIControlStateHighlighted)];    [deviceBtn setBackgroundColor:[UIColor yellowColor]];    [deviceBtn setFrame:CGRectMake(120, 50, 200, 30)];    [deviceBtn addTarget:self action:@selector(getDeviceInfo) forControlEvents:(UIControlEventTouchUpInside)];    [self.view addSubview:deviceBtn];        UIButton *openCameraBtn =[[UIButton alloc]init];    [openCameraBtn setTitle:@"打开相机" forState:(UIControlStateNormal)];    [openCameraBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];    [openCameraBtn setTitleColor:[UIColor blueColor] forState:(UIControlStateHighlighted)];    [openCameraBtn setBackgroundColor:[UIColor yellowColor]];    [openCameraBtn setFrame:CGRectMake(330, 50, 200, 30)];    [openCameraBtn addTarget:self action:@selector(openCamera) forControlEvents:(UIControlEventTouchUpInside)];    [self.view addSubview:openCameraBtn];        }- (void)getDeviceInfo{    NSLog(@"获取设备信息。。。。");    NSString *name = [[UIDevice currentDevice] name];    NSString *systemName = [[UIDevice currentDevice] systemName];    NSString *systemVersion = [[UIDevice currentDevice] systemVersion];    NSString *model = [[UIDevice currentDevice] model];    NSString *localizeModel = [[UIDevice currentDevice] localizedModel];        UILabel *nameL = [[UILabel alloc] init];    UILabel *systemNameL = [[UILabel alloc] init];    UILabel *systemVersionL = [[UILabel alloc] init];    UILabel *modelL = [[UILabel alloc] init];    UILabel *localizeModelL = [[UILabel alloc] init];        [nameL setText:name];    [systemNameL setText:systemName];    [systemVersionL setText:systemVersion];    [modelL setText:model];    [localizeModelL setText:localizeModel];        [nameL setTextColor:[UIColor blueColor]];    [systemNameL setTextColor:[UIColor blueColor]];    [systemVersionL setTextColor:[UIColor blueColor]];    [modelL setTextColor:[UIColor blueColor]];    [localizeModelL setTextColor:[UIColor blueColor]];        CGFloat x = 10;    CGFloat y = 80;    CGFloat width = 200;    CGFloat height=20;        nameL.frame = CGRectMake(x, y+20, width, height);    systemNameL.frame = CGRectMake(x, y+40, width, height);    systemVersionL.frame = CGRectMake(x, y+60, width, height);    modelL.frame = CGRectMake(x, y+80, width, height);    localizeModelL.frame = CGRectMake(x, y+100, width, height);        [self.view addSubview:nameL];    [self.view addSubview:systemNameL];    [self.view addSubview:systemVersionL];    [self.view addSubview:modelL];    [self.view addSubview:localizeModelL];}- (void)openCamera{    //NSLog(@"打开摄像头。。。。");    //UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];    self.imagePicker.editing = YES;    self.imagePicker.delegate = self;    self.imagePicker.allowsEditing = YES;        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){        NSLog(@"选择相机。。。");        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;    }        [self presentViewController:self.imagePicker animated:YES completion:nil];}@end

这两个文件其实是我已经在ios原生项目下编译运行过的文件,然后被CameraDemo.m调用。(其实有点类似于库的作用)

直白一点就是。有一个库(ViewController.h和ViewController.m),提供了一个类ViewController,这个类提供了两个方法

  • (void)getDeviceInfo; //获取ios设备信息

  • (void)OpenCamera; //打开ios相机

然后CameraDemo.m去实例化了这个类
CameraDemo.m

#import <Cordova/CDV.h>#import "ViewController.h"//这里必须继承CDVPlugin 类,表示CameraDemo是Cordova插件类@interface CameraDemo : CDVPlugin {  // Member variables Go here.}@property (nonatomic,strong) ViewController *view;  //声明一个ViewController- (void)coolMethod:(CDVInvokedUrlCommand*)command;  //创建插件自带的方法,可以删除- (void)openCamera:(CDVInvokedUrlCommand*)command;@end@implementation CameraDemo- (void)pluginInitialize{    NSLog(@"===========================初始化CameraDemo");    [super pluginInitialize];    // 实例化ViewController     self.view = [[ViewController alloc] init];}//创建插件自带的方法,可以删除- (void)coolMethod:(CDVInvokedUrlCommand*)command{    CDVPluginResult* pluginResult = nil;    NSString* echo = [command.arguments objectAtIndex:0];    if (echo != nil && [echo length] > 0) {        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];    } else {        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];    }    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];}- (void)openCamera:(CDVInvokedUrlCommand*)command{// 将ViewController的实例viewController  显示出来    [self.viewController presentViewController:self.view animated:YES completion:nil];    //ViewController *view = [[ViewController alloc] init];    //[view openCamera];    //CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];;    //[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];}@end

CameraDemo.js

var exec = require('cordova/exec');exports.coolMethod = function (arg0, success, error) {    exec(success, error, 'CameraDemo', 'coolMethod', [arg0]);};exports.openCamera = function (arg0, success, error) {    exec(success, error, 'CameraDemo', 'openCamera', [arg0]);};

plugin.xml (这个文件非常非常的重要,js可以调用oc全靠它,多查查资料)

<?xml version='1.0' encoding='utf-8'?><plugin id="cordova-plugin-camerademo" version="1.0.0"    xmlns="Http://apache.org/cordova/ns/plugins/1.0"    xmlns:Android="http://schemas.android.com/apk/res/android">    <name>CameraDemo</name>    <js-module name="CameraDemo" src="www/CameraDemo.js">        <clobbers target="cordova.plugins.CameraDemo" />    </js-module>    <platform name="ios">        <config-file parent="/*" target="config.xml">            <feature name="CameraDemo">                <param name="ios-package" value="CameraDemo" onload="true"/>            </feature>        </config-file>        <source-file src="src/ios/CameraDemo.m" />        <header-file src="src/ios/ViewController.h" />        <source-file src="src/ios/ViewController.m" />    </platform></plugin>

package.json (一般不需要修改)

{  "name": "cordova-plugin-camerademo",  "version": "1.0.0",  "description": "",  "cordova": {    "id": "cordova-plugin-camerademo",    "platforms": [      "ios"    ]  },  "keyWords": [    "ecosystem:cordova",    "cordova-ios"  ],  "author": "",  "license": "ISC"}

CameraDemo.js 通过 plugin.xml 配置去调用了原生的ocject-c方法

最后

Cordova项目调用插件

重要,如果调用和插件中的plugin.xml配置有关,所以plugin.xml非常重要

// 在项目的 ts文件中调用declare let cordova:anycordova.plugins.CameraDemo.openCamera();

感谢各位的阅读!关于“怎么在IOS中使用Cordova插件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: 怎么在IOS中使用Cordova插件

本文链接: https://www.lsjlt.com/news/270886.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MySQL怎么更新字段长度
    要更新MySQL表中的字段长度,可以使用ALTER TABLE语句。以下是更新字段长度的示例: ALTER TABLE table_...
    99+
    2024-05-14
    mysql
  • sql如何修改表字段长度
    要修改表字段的长度,可以使用ALTER TABLE语句,然后使用MODIFY COLUMN来修改字段的长度。以下是一个示例: ALT...
    99+
    2024-05-14
    sql
  • mysql数据量太大如何解决
    有几种方法可以解决MySQL数据量过大的问题: 数据库分区:将数据库表按照某种规则进行分区,分成多个小表,可以提高查询速度和管理...
    99+
    2024-05-14
    mysql
  • MySQL单表数据量过大如何解决
    在MySQL中,当单表的数据量过大时,可以采取以下几种方法来解决问题: 使用索引:在表的字段上创建适当的索引,可以提高查询效率,...
    99+
    2024-05-14
    mysql
  • mysql导出数据量过大如何解决
    当导出的数据量过大时,可以尝试以下方法来解决问题: 使用数据库工具:使用数据库管理工具如Navicat或MySQL Workbe...
    99+
    2024-05-14
    mysql
  • mysql中log_bin的作用是什么
    在MySQL中,log_bin是二进制日志文件的名称,用于记录所有对数据库进行更改的操作,如插入、更新、删除等。这些二进制日志文件可...
    99+
    2024-05-14
    mysql
  • mysql中log_bin的特点有哪些
    log_bin是MySQL的二进制日志文件,用于记录所有对数据库的修改操作,包括插入、更新、删除等。 log_bin文件是M...
    99+
    2024-05-14
    mysql
  • mysql rpm包安装的方法是什么
    要在Linux系统上安装MySQL RPM包,可以按照以下步骤操作: 下载MySQL的RPM包:你可以从MySQL官方网站下载适...
    99+
    2024-05-14
    mysql
  • mysql rpm包安装后怎么使用
    安装MySQL RPM包后,您可以通过以下步骤来使用MySQL: 启动MySQL服务:使用以下命令来启动MySQL服务: sud...
    99+
    2024-05-14
    mysql
  • lxml中怎么处理XML命名空间默认值
    在lxml中处理XML命名空间的默认值可以通过使用xpath()方法和register_namespace()方法来实现。...
    99+
    2024-05-14
    lxml
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作