sdeleon28
sdeleon28

Reputation: 329

React Native doesn't pick up native iOS module

I'm trying to set up a native iOS module for React Native with Swift.

Here's my Swift file:

// SwiftManager.swift
import Foundation

@objc(SwitchManager)
class SwitchManager: NSObject {
  @objc func addEvent(_ name: String) -> Void {
    NSLog("It works!")
  }
}

And here is my implementation file:

// SwiftManager.m

#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(SwitchManager, NSObject)

RCT_EXTERN_METHOD(addEvent:(NSString *)name)

@end

Then, I try to use it in my React Native code like so:

// index.ios.js
import React, { Component } from 'react';
import { NativeModules } from 'react-native';
const SwitchManager = NativeModules.SwitchManager;
SwitchManager.addEvent('Birthday Party');
// ...

But SwitchManager ends up being undefined. In another project I put together, I have successfully created a native module and I can't recall doing anything different. Do you have any ideas on why RN wouldn't pick up this native module?

Upvotes: 0

Views: 328

Answers (1)

sdeleon28
sdeleon28

Reputation: 329

I reverted all changes to a clean version of my repo and created the files from scratch again in Xcode. React Native is able to pick up the native module now. I think the problem could have been related to renaming things around and Xcode not keeping references properly, although I can't say for sure because I had several changes in my working tree.

Upvotes: 1

Related Questions