Home > Back-end >  Bridged native module not showing in React Native [macOS]
Bridged native module not showing in React Native [macOS]

Time:09-26

I made (actually, not complete) a module that bridges NSOpenPanel and NSSavePanel. Here's my RCTPanel.m:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "RCTPanel.h"

@implementation RCTPanel

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(open)
{
  NSOpenPanel *panel;
  
  if ([panel runModal] == NSModalResponseOK)
  {
    // TODO
  }
}

RCT_EXPORT_METHOD(save)
{
  NSSavePanel *panel;

  if ([panel runModal] == NSModalResponseOK)
  {
    //TODO
  }
}

and here's my testing code App.tsx:

import React from 'react';
import { Button, NativeModules, View } from "react-native";

export default class App extends React.Component {
    render() {
        return (
            <View>
                <Button title='OPEN' onPress={ () => NativeModules.Panel.open() }/>
                <Button title='SAVE' onPress={ () => NativeModules.Panel.save() }/>
            </View>
        );
    }
}

When I run npx react-native run-macos, the app shows fine, but when I press buttons which I made, Open/Save panel does not shows up. no error message.

I first thought asking to Microsoft(which maintains react-native-macos),
but it doesn't seems like a bug. more to be my mistake. so I'm asking here.

CodePudding user response:

NSOpenPanel *panel;
  
if ([panel runModal] == NSModalResponseOK)
{
    // TODO
}

Note that your first line only designates a panel variable, it doesn't actually create the panel itself. In manual-reference-counting, it would be a junk (uninitialized) pointer, but may be nil in ARC. In any case, sending the -runModal message to nil is simply ignored and nothing happens, which is the behavior you describe.

Change the lines to NSOpenPanel *panel = [NSOpenPanel openPanel]; and NSSavePanel *panel = [NSSavePanel savePanel]; and they should work properly.

In Objective-C, if things don't happen like they should, it's always a good idea to check to see if some value that you think exists is not, in fact, nil.

  • Related