电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

如何在IOS中使用Cordova插件

瀏覽:105日期:2022-09-16 17:13:58
一、準備

插件功能:打開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.json1:使用命令 “npm init” 創建package.json文件2:plugman createpackagejson [插件路徑]原應用使用的ionic UI框架,沒有package.json無法安裝插件

最終插件目錄結構

如何在IOS中使用Cordova插件

除了ViewController.h和ViewController.m文件,其余的文件通過上述步驟都會自動生成

二、過程

創建文件ViewController.h和ViewController.mViewController.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

/********* CameraDemo.m Cordova Plugin Implementation *******/#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 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='http://www.hdgsjgj.cn/bcjs/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='http://www.hdgsjgj.cn/bcjs/src/ios/CameraDemo.m' /><header-file src='http://www.hdgsjgj.cn/bcjs/src/ios/ViewController.h' /><source-file src='http://www.hdgsjgj.cn/bcjs/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插件的詳細內容,更多關于IOS使用Cordova的資料請關注好吧啦網其它相關文章!

標簽: IOS
相關文章:
主站蜘蛛池模板: 茶叶百科网-茶叶知识与茶文化探讨分享平台 | 高温高压釜(氢化反应釜)百科| 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 广州云仓代发-昊哥云仓专业电商仓储托管外包代发货服务 | 飞歌臭氧发生器厂家_水处理臭氧发生器_十大臭氧消毒机品牌 | 安规_综合测试仪,电器安全性能综合测试仪,低压母线槽安规综合测试仪-青岛合众电子有限公司 | 挤出熔体泵_高温熔体泵_熔体出料泵_郑州海科熔体泵有限公司 | 保镖公司-私人保镖-深圳保镖公司【环宇兄弟保镖】 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛婚外情取证-青岛王军侦探事务所 | 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | Brotu | 关注AI,Web3.0,VR/AR,GPT,元宇宙区块链数字产业 | 茶叶百科网-茶叶知识与茶文化探讨分享平台| 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 挤奶设备过滤纸,牛奶过滤纸,挤奶机过滤袋-济南蓝贝尔工贸有限公司 | VI设计-LOGO设计公司-品牌设计公司-包装设计公司-导视设计-杭州易象设计 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 | 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | (中山|佛山|江门)环氧地坪漆,停车场地板漆,车库地板漆,聚氨酯地板漆-中山永旺地坪漆厂家 | 步入式高低温测试箱|海向仪器| 铁素体测量仪/检测仪/铁素体含量测试仪-苏州圣光仪器有限公司 | 智慧消防-消防物联网系统云平台| 飞象网 - 通信人每天必上的网站| 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 医用酒精_84消毒液_碘伏消毒液等医用消毒液-漓峰消毒官网 | 石英粉,滑石粉厂家,山东滑石粉-莱州市向阳滑石粉有限公司 | 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | 压力变送器-上海武锐自动化设备有限公司 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 爆炸冲击传感器-无线遥测传感器-航天星百科 |