I wrote a native wrapper for text recognition using Google MLKit for ios in swift language. the recognizing part working fine. but when I use the method in react native (javascript), the catch block is called immediately after then block. am I missing somethin here?. Here are my native swift module files:
ios/Podfile
# To recognize Latin script
pod 'GoogleMLKit/TextRecognition'
ios/MyApp-Bridging-Header.h
#import "React/RCTBridgeModule.h"
ios/OCRTextRecognizer.m
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"
@interface RCT_EXTERN_MODULE(OCRTextRecognizer,NSObject)
RCT_EXTERN_METHOD(runTextRecognition:(NSString)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
@end
ios/OCRTextRecognizer.swift
import Foundation
import MLKit
import UIKit
@objc(OCRTextRecognizer)
class OCRTextRecognizer: NSObject{
// When using Latin script recognition SDK
let latinOptions = TextRecognizerOptions()
private lazy var textRecognizer = TextRecognizer.textRecognizer(options: latinOptions)
@objc(runTextRecognition:resolver:rejecter:)
func runTextRecognition(_ url:String, resolver resolve:@escaping RCTPromiseResolveBlock, rejecter reject:@escaping RCTPromiseRejectBlock) {
let imagePath = url
DispatchQueue.global(qos: .default).async{
if let imagee = UIImage(contentsOfFile: imagePath){
let visionImage = VisionImage(image: imagee)
visionImage.orientation = imagee.imageOrientation
self.textRecognizer.process(visionImage) {result,error in
guard error == nil, let result = result else {
// Error handling
DispatchQueue.main.async {
reject("TEXT_RECOGNITION_ERROR","Error recognition text",error)
}
return
}
// Recognized text
var resLines:[String] = []
for block in result.blocks {
for line in block.lines {
resLines.append(line.text)
}
}
let response = ["blocks":resLines]
DispatchQueue.main.async {
resolve(response)
}
}
}
}
}
@objc
static func requiresMainQueueSetup() -> Bool {
return true
}
}
and finally I used the native method in my React Native component like this:
import {NativeModules} from 'react-native'
NativeModules.OCRTextRecognizer.runTextRecognition(imagePath)
.then((response: any) => {
console.log('This block called first');
})
.catch((error: any) => {
console.log('then this block called');
});
what I expect is when there is a response (recognized texts), only then block called
what I expect is when there is a response (recognized texts), only then block called