Access Library's Swift methods inside Project's AppDelegate.mm (Turbo Modules) #554
Replies: 1 comment 3 replies
-
I've found the solution that is currently working in a project with the latest React Native release and use_frameworks! So here we go: Steps
Done! You can now leverage your methods defined in ModuleName.h, inside AppDelegate.mm. Basically we need the ModuleName.mm file to act as a bridge between our ModuleName.swift implementation and the Project's AppDelegate.mm. ExampleFirstWe define the following two methods inside ModuleName.h, that we want to be able to use inside AppDelegate.mm. + (void)setSelectedOrientationMask: (UIInterfaceOrientationMask)orientation;
+ (UIInterfaceOrientationMask)getSelectedOrientationMask;
SecondWe then import xcode auto generated swift header to allow the use of our ModuleName.swift inside our ModuleName.mm code #if __has_include("react_native_orientation_manager-Swift.h")
#import "react_native_orientation_manager-Swift.h"
#else
#import "react_native_orientation_manager/react_native_orientation_manager-Swift.h"
#endif
ThirdWe then init our swift class as a static property inside ModuleName.mm. static OrientationManagerImpl *_orientation = [[OrientationManagerImpl alloc] init];
FourthWe implement requiresMainQueueStep to allow our swift implementation init. + (BOOL)requiresMainQueueSetup
{
return YES;
}
FifthWe implement all of our methods defined inside ModuleName.h, as you can see we use our ModuleName.swift instance. + (void)setSelectedOrientationMask: (UIInterfaceOrientationMask)mask
{
[_orientation setSelectedOrientationMaskWithMask:mask];
}
+ (UIInterfaceOrientationMask)getSelectedOrientationMask {
return [_orientation getSelectedOrientationMask];
}
SixthWe can now import our ModuleName.h inside AppDelegate.mm and use our ModuleName.mm that calls our ModuleName.swift under the hood. #import "OrientationManager.h"
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return [OrientationManager getSelectedOrientationMask];
}
ConclusionsSo now we have a way to create Swift modules that are callable both from JS and our react native project. |
Beta Was this translation helpful? Give feedback.
-
Hi everyone!
First things first: I'm a true newbie when it comes to iOS native development, so take everything I write with a lot of doubt, because I am not even sure that it could be done in the first place.
I'm trying to develop a local library that should allow us to "simply" lock the orientation to either portrait or landscape depending the business logic.
So the idea is: "By default i want every screen to be in Portrait, while a given screen B, for example, should be able to follow the screen orientation, therefore we must allow Landscape mode too".
I've managed to properly set it up using Kotlin and it does work, however the same can't be said for Swift.
Taking a step back: "Why don't you use Objective C?" simply put: "I'd like to develop something using a modern approach, because there is a clean syntax and more documentation / examples in the wild west"
That said, I've created the following project structure:
Orientation.m
Orientation.swift
Orientation-Bridging-Header.h
I've updated the podspec to handle .swift files too during installation.
So, know my next step would be to import Orientation.swift inside AppDelegate.mm. Surfing the internet I've found that normally, this is done using Xcode feature: auto generated *-Swift.h file, that's great really, if only it would work.
We should be able to import such file using the syntax that you can see in the following Project's AppDelegate.mm:
Although XCode complains with the following error related to my Orientation-Swift.h import:
orientation/example/ios/OrientationExample/AppDelegate.mm:4:9: fatal error: 'Orientation-Swift.h' file not found
#import "Orientation-Swift.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Does anyone know how we can let xcode do his job and generate such file?
Any help would be really appreciated.
And please bear in mind that my iOS development knowledge is very limited so I might ask you to further explain something if it does appear to be complex or confusing for me.
Again thanks!
Beta Was this translation helpful? Give feedback.
All reactions