Unknown type name 'MyAppPushNotificationHandler'

Viewed 33

I'm creating a react native app, but need to make some adjustments in native iOS code to support push notifications when integrating a CRM system.

I've created a swift file (according to the guide here) that's bridged to Object-C code.

The Swift file is imported like this: #import "app-Swift.h"

and declared like this MyAppPushNotificationsHandler* pnHandlerObj = [[MyAppPushNotificationsHandler alloc] init];

If I:

  • add it directly after the "app-Swift.h" I get the error Use of undeclared identifier 'pnHandlerObj' further down in the file where I'm using it
  • if I add it just above the didFinishLaunchingWithOptions (below @interface and @implementation AppDelegate) I get the two errors Use of undeclared identifier 'MyAppPushNotificationsHandler' and Unknown type name 'MyAppPushNotificationsHandler'

Does anyone know what I'm missing here? Thanks a lot.

1 Answers

It seems like the file is not being imported properly. Based on this line #import "app-Swift.h" I can conclude that the Product Name of your app is app. Maybe that's incorrect, you should be sure that you're using the correct Product Name. To check the Product Name of your app go to Targets > Build Settings:

enter image description here

The Product Name on this specific image is AutolayoutDemoView. So in this instance you would import the Swift file inside of AppDelegate.m like this:

#import "AutolayoutDemoView-Swift.h"

Another note

The guide tells you to initialize the class MyAppPushNotificationsHandler outside of any function, like this:

MyAppPushNotificationsHandler* pnHandlerObj = [[MyAppPushNotificationsHandler alloc] init];

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
...

But if you do that, you will get the following error: Initializer element is not a compile-time constant

So to get rid of that error, you should initialize the MyAppPushNotificationsHandler inside of the didFinishLaunchingWithOptions function:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 { 
   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
   UIViewController *rootViewController = [UIViewController new];
   rootViewController.view = rootView;
   self.window.rootViewController = rootViewController;
   [self.window makeKeyAndVisible];
   
   // Register for push notifications
   MyAppPushNotificationsHandler* pnHandlerObj = [[MyAppPushNotificationsHandler alloc] init];
   [pnHandlerObj registerPushNotification:self];
   return YES;
 }
Related