时间:2021-05-19
插件功能:打开IOS相机
plugman create --name [插件名称] --plugin_id [插件ID] --plugin_version [插件版本号]
plugman create --name CameraDemo --plugin_id cordova-plugin-camerademo --plugin_version 1.0.0
plugman platform add --platform_name ios
以下两种都可以生成package.json
1:使用命令 “npm init” 创建package.json文件
2:plugman createpackagejson [插件路径]
原应用使用的ionic UI框架,没有package.json无法安装插件
最终插件目录结构
除了ViewController.h和ViewController.m文件,其余的文件通过上述步骤都会自动生成
创建文件ViewController.h和ViewController.m
ViewController.h
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,这个类提供了两个方法
然后CameraDemo.m去实例化了这个类
CameraDemo.m
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方法
重要,如果调用和插件中的plugin.xml配置有关,所以plugin.xml非常重要
// 在项目的 ts文件中调用declare let cordova:anycordova.plugins.CameraDemo.openCamera();以上就是如何在IOS中使用Cordova插件的详细内容,更多关于IOS使用Cordova的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Gradle插件代码可以在build.gradle中,buildSrc项目中,以及独立的插件项目中编写。本文将介绍如何在一个独立的项目中使用Java语言编写Gr
在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何
如何在易语言中使用类型库如何在易语言中使用类型库,我们下面用一个小例程来说明,首先打开“易语言”选择“工具”→“类型库及OCX组件—〉支持库”注册word类型库
前言ionic是一个垮平台开发框架,可通过web技术开发出多平台的应用。但只建议开发简单应用。复杂的应用需要用到许多cordova插件,而cordova插件的更
起步在《分布式任务队列Celery使用说明》中介绍了在Python中使用Celery来实验异步任务和定时任务功能。本文介绍如何在Django中使用Celery。