react-nativenative-module

React Native NativeModules Empty Object


Trying to create my first npm module in order to determine if a user is on the phone. It's for a React Native app. No matter what I try, the module returns undefined. NativeModules always appears to be an empty object.

Please help! Below is a link to the code. export default RNOnPhoneCall; in index.js will only return undefined. How do I link the functions in ios folder, and export them in index.js? Heads up, android is not up to date yet, only ios.

Link to Github


Solution

  • I was not exporting the method correctly in my iOS file. Here's the solution, along with a link to the final project.

    react-native-check-phone-call-status on github

    #import "RNCheckPhoneCallStatus.h"
    #import "React/RCTLog.h"
    #import <AVFoundation/AVAudioSession.h>
    #import<CoreTelephony/CTCallCenter.h>
    #import<CoreTelephony/CTCall.h>
    
    @implementation RNCheckPhoneCallStatus
    
    RCT_EXPORT_MODULE()
    
    RCT_EXPORT_METHOD(get:(RCTResponseSenderBlock)callback)
    {
        NSString *phoneStatus = @"PHONE_OFF";
        CTCallCenter *ctCallCenter = [[CTCallCenter alloc] init];
        if (ctCallCenter.currentCalls != nil)
        {
            NSArray* currentCalls = [ctCallCenter.currentCalls allObjects];
            for (CTCall *call in currentCalls)
            {
                if(call.callState == CTCallStateConnected)
                {
                    phoneStatus = @"PHONE_ON";
                }
            }
        }
        callback(@[[NSNull null], phoneStatus]);
    }
    
    @end