TurboModules - resolve or reject return null in RN 0.72.3 #427
-
DescriptionDescriptionI'm using a boilerplate to create a library using the turbo module, the implementation in the Android part was a success, but the implementation in the IOS part has been giving me a headache, the function without Promise (resolve or reject) is working and returning the values, but the function with resolve is returning null or does not finish. it will be a bug of the new architecture. code typescript spec import type {TurboModule} from 'react-native';
import {TurboModuleRegistry} from 'react-native';
export type CameraPermissionStatus =
| 'authorized'
| 'not-determined'
| 'denied'
| 'restricted';
export type CameraPermissionRequestResult = 'authorized' | 'denied';
export type EventCameraName = 'CameraPermission';
export interface Spec extends TurboModule {
getCameraPermissionStatus(): CameraPermissionStatus;
requestCameraPermission(): Promise<CameraPermissionRequestResult>;
}
export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativePermissions'); code index.ts import type {
CameraPermissionStatus,
CameraPermissionRequestResult,
} from './NativeReactNativePermissions';
export * from './hooks/useCameraPermission';
const ReactNativePermissions =
require('./NativeReactNativePermissions').default;
export function getCameraPermissionStatus(): CameraPermissionStatus {
return ReactNativePermissions.getCameraPermissionStatus();
}
export function requestCameraPermission(): Promise<CameraPermissionRequestResult> {
return ReactNativePermissions.requestCameraPermission();
} code ReactNativePermissions.h #ifdef RCT_NEW_ARCH_ENABLED
#import "RNReactNativePermissionsSpec.h"
@interface ReactNativePermissions : NSObject <NativeReactNativePermissionsSpec>
#else
#import <React/RCTBridgeModule.h>
@interface ReactNativePermissions : NSObject <RCTBridgeModule>
#endif
@end code ReactNativePermissions.mm #import <AVFoundation/AVFoundation.h>
#import <React/RCTLog.h>
#import "RNReactNativePermissionsSpec.h"
#import "ReactNativePermissions.h"
@implementation ReactNativePermissions
RCT_EXPORT_MODULE()
// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (NSString *)getCameraPermissionStatus {
RCTLogInfo(@"test");
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(status == AVAuthorizationStatusAuthorized) {
return @"authorized";
} else if(status == AVAuthorizationStatusDenied) {
return @"denied";
} else if(status == AVAuthorizationStatusRestricted) {
return @"restricted";
}
return @"not-dethermined";
}
- (void)requestCameraPermission: resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
RCTLogInfo(@"test");
NSString *result = @"1";
resolve(result);
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeReactNativePermissionsSpecJSI>(params);
}
#endif
@end code App.tsx import React from 'react';
import {StyleSheet, View, Text, TouchableOpacity} from 'react-native';
import {requestCameraPermission} from '@brainylab/react-native-permissions';
export default function App() {
// const {status, requestPermission} = useCameraPermission();
const handleButton = async () => {
console.log('init');
const result = await requestCameraPermission();
console.log('finally', result);
};
return (
<View style={styles.container}>
{/* <Text>Result: {status}</Text> */}
<TouchableOpacity style={styles.button} onPress={handleButton}>
<Text>Request Permission</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
button: {
marginTop: 40,
backgroundColor: 'green',
padding: 10,
},
}); console.log in App.tsx const handleButton = async () => {
console.log('init');
const result = await requestCameraPermission();
console.log('finally', result);
}; React Native Version0.72.3 Output of
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
One issue I see is the type mismatch. On typescript, you have added a custom type that returns with your promise but you are resolving that promise with a string on ios. Can you change it to be |
Beta Was this translation helpful? Give feedback.
-
I already did, error persists |
Beta Was this translation helpful? Give feedback.
-
It seems like your function signature is wrong. I would have two recommendations for further development:
|
Beta Was this translation helpful? Give feedback.
-
thanks for the help, i'm newbie in native code world 🎉🎉 |
Beta Was this translation helpful? Give feedback.
It seems like your function signature is wrong.
I have created a PR for the original repository and I'm going to turn this into a discussion. Please check: https://github.com/brainylab/react-native-permissions/pull/1/files
I would have two recommendations for further development: