React Native NativeModules Empty Object
Asked Answered
S

3

6

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

Sipe answered 29/8, 2017 at 23:46 Comment(0)
S
1

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
Sipe answered 6/9, 2017 at 22:29 Comment(1)
Also if you are following the react tutorial it is a bit misleading. It will work with debug mode turned on. If you want it to work without debug mode you need the callback mentioned here.Fantasize
Q
8

After snooping around a little, I found out that the Obj-C file isn't added to the Compile Sources in Xcode. Just follow me: Let's say the file names are: NotchNative.h and NotchNative.m. If the NotchNative.m file is not listed, the files will not be compiled by Xcode hence it needs to be added to the Compile Sources.

enter image description here

Follow the steps in the image and rebuild/run the iOS app again. It's surely a fault on the Xcode side, as it doesn't happen in case of Swift projects.

Quadrillion answered 16/6, 2021 at 11:36 Comment(1)
Thanks so much. No wonder the RN docs recommend creating and editing files via XCode.Cairns
C
3

Try rebuilding your project. I faced the same issue recently and simply rebuilding resolved it

Cankerworm answered 6/9, 2017 at 10:5 Comment(0)
S
1

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
Sipe answered 6/9, 2017 at 22:29 Comment(1)
Also if you are following the react tutorial it is a bit misleading. It will work with debug mode turned on. If you want it to work without debug mode you need the callback mentioned here.Fantasize

© 2022 - 2024 — McMap. All rights reserved.