之前项目中iOSNative 需要主动给JS 发送事件的时候一直使用的是下面这种方式:
[self.bridge.eventDispatcher sendAppEventWithName:@"QRCodeWithPhoto" body:@{
@"scanResult":scandResult,
@"stateCode":@(self.stateCode),
}];
这种方式虽然还可以用,但是后面一直会有个黄色的警告,提示该方法已经被遗弃,可以使用RCTEventEmitter代替。为了后面项目的稳定,所以决定使用新的API 去实现该功能。不过网上对于RCTEventEmitter的资料实在太少,react native 的官网文档上对于native 给 JS 发送事件使用的还是上面那种被遗弃的方法。所以就按照网上查找的一些英文资料进行尝试。OC 层的话还是定义一个类,继承自 RCTEventEmitter:
#import "RCTEventEmitter.h"
#import "RCTBridge.h"
@interface NativeModule : RCTEventEmitter<RCTBridgeModule>
@end
.m文件
#import "NativeModule.h"
#import "AppDelegate.h"
@interface NativeModule()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic ,strong) UIViewController * showViewController;
@property (nonatomic ,assign) int stateCode; // 状态码,0:代表获取二维码成功,-1:代表失败或者没有二维码
@end
@implementation NativeModule
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)supportedEvents
{
return @[@"QRCodeFromPhoto"]; // 这里注意要和 [self sendEventWithName:@"QRCodeFromPhoto" body:@{ @"scanResult":scandResult, @"stateCode":@(self.stateCode), }]; 里面发送的事件名字一致,不然JS 端不会识别
}
RCT_EXPORT_METHOD(QRCode)
{
UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES; //设置选中的照片可被编辑(设置的话点照片会进入大图预览界面,二维码功能不需要进入大图,所以注释掉)(这里设置为禁用的话虽然可以不进入大图预览编辑界面,但是会导致手机拍的照片不能识别(远距离拍的可以识别,近距离的不可以识别))
// 将来源设置为相册
/**
UIImagePickerControllerSourceTypePhotoLibrary,相册
UIImagePickerControllerSourceTypeCamera,相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片库
*/
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[[self getShowViewController] presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage * image = info[UIImagePickerControllerOriginalImage];
// 初始化一个检测器
CIDetector * detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
[picker dismissViewControllerAnimated:YES completion:^{
//设置数组,放置识别完之后的数据
NSArray * resultArray = [detector featuresInImage:[CIImage imageWithData:UIImagePNGRepresentation(image)]];
NSString * scandResult;
//判断是否有数据(即是否是二维码)
if (resultArray.count >= 1) {
//取第一个元素就是二维码所存放的文本信息
CIQRCodeFeature * feature = resultArray[0];
scandResult = feature.messageString;
self.stateCode = 0;
}else
{
self.stateCode = -1;
scandResult = @"该图片不包含二维码信息";
}
dispatch_async(dispatch_get_main_queue(), ^{
[self sendEventWithName:@"QRCodeFromPhoto" body:@{
@"scanResult":scandResult,
@"stateCode":@(self.stateCode),
}];
});
}];
}
- (UIViewController *)getShowViewController
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIWindow * window = delegate.window;
if ([window.rootViewController isKindOfClass:[UINavigationController class]]) {
_showViewController = ((UINavigationController *)window.rootViewController).visibleViewController;
}else if ([window.rootViewController isKindOfClass:[UITabBarController class]])
{
_showViewController = ((UITabBarController *)window.rootViewController).selectedViewController;
}else
{
_showViewController = window.rootViewController;
}
return _showViewController;
}
@end
** JS 端调用的话 **