How to access a method in a class written in swift using the app delegate
I am a newbie for objc. I have written a class exposing it to the JS side using swift. The content in that file looks like as follows,
import Foundation
@objc(AppLinkModule)
class AppLinkModule: NSObject{
@objc
static var appLink: String?
@objc
func setLink(link: String){
AppLinkModule.appLink = link;
}
@objc
func getLink(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
resolve(AppLinkModule.appLink);
AppLinkModule.appLink = nil;
}
}
The AppLinkModule.m file is as follows,
#import "React/RCTBridgeModule.h"
@interface RCT_EXTERN_MODULE(AppLinkModule, NSObject)
RCT_EXTERN_METHOD(getLink)
@end
What I actually want is to call the setLink method from the AppDelegate.m file or else directly set to the static variable appLink. Some instructions to achieve this will be greatly appreciated.
This looks fine.
If you want to call getLink()
from your App Delegate, you'll need to import the Auto-generated Swift Bridging Header File that Xcode creates when you compile your code. This will allow the Objective-c AppDelegate.m
file to find your exposed swift code.
#import <ProjectName-Swift.h>
will allow your Swift to be exposed to the AppDelegate, and you already have marked the class and functions @objc
so they will be visible.
Then you just need to either create a singleton or shared instance of your AppLinkModule (React Native Modules are all run statically) or you need to instantiate your module class.
Once you've done so, you can call this function in the AppDelegate like so:
[[[AppLinkModule ] shared ] getLink];