Importing Swift classes from a react-native-library into an example app written in Objective-C

Viewed 393

Background

I am building a react-native-library created from @react-native-community/bob with Swift for iOS and Kotlin for Android. The template provides an example app to test the module/library created. The example app is built in Objective-C. I am trying to import a Swift class to open a URL and therefore implemented the following function in swift:

@objc func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { }

which is part of a class as such:

@objc(SwiftClassName) open class SwiftClassName: NSObject { 
    @objc func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { }
}

Problem

This function must be implemented within the Objective-C AppDelegate.m file within my example app in-order for the URL to be opened and the instance returned to Swift, as such:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)URL options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    return [[SwiftClassName sharedInstance] app:app openURL:URL options:options];
}

Issue

Everything is linked correctly and I can see the module/library within my example app but I cannot call the class which implements the function (i.e. SwiftClassName)

What I've Tried

NOTE: The ProjectName and ProjectModuleName are the same for my project

Within my AppDelegate.h file import the following:

@class SwiftClassName

Within my AppDelegate.m file import the following:

#import <ProjectName/ProjectModuleName-Swift.h>

The above #import statement is where the errors start.

  • Whether I try #import <ProjectName/ProjectModuleName-Swift.h> or #import "ProjectModuleName-Swift.h" I get a Lexical or Preprocessor Issue and of course File Not Found
  • If I remove the #import statement, since I declare @class SwiftClassName in AppDelegate.h I get the errors:
    • Receiver type 'SwiftClassName' for instance message is a forward declaration
    • Receiver 'SwiftClassName' for class message is a forward declaration

Within the Swift Xcode Project I have the following checked for both the PROJECT and TARGET:

  • Derives Modules → YES
  • Always Embed Swift Standard Library → YES
  • Install Objective-C Compatibility Headers → YES

Question

How can I get the Objective-C example app to access my Swift class so that I can implement the necessary callback function within my app?

My Guess

It seems as though the library project does not create the TargetName-Swift.h file and as such I can not implement the function. But that's just my guess. I can't see it anywhere within the folder:

~/Library/Developer/Xcode/
    DerivedData/
        CurrentXcodeProject-ducoaakebhutnrdndbmrrakyafki/
            Build/
                Intermediates.noindex/
                    CurrentXcodeProject.build/
                        Debug-iphonesimulator/
                            CurrentXcodeProject.build/

I have read that under Debug-iphonesimulator/CurrentXcodeProject.build/ I should be seeing a DerivedData folder containing the mentioning TargetName-Swift.h file but there is no such thing.

Info

OS: macOS
React-Native Version: 0.62.2

Please let me know if you require any additional information to find a solution to this issue as I have literally come to a dead end.

0 Answers
Related