You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native集成CRM推送通知:iOS原生代码类型未定义问题求助

React Native iOS推送集成原生代码错误修复方案

两种错误的根源及解决办法

错误1:Use of undeclared identifier 'pnHandlerObj'

这是变量作用域问题导致的:

  • 如果你把变量声明放在#import "app-Swift.h"之后、@implementation AppDelegate之前,即便作为全局变量,跨方法访问时也容易出现作用域失效问题。更稳妥的做法是将其设为AppDelegate的实例变量:
    在AppDelegate.m中添加(没有@interface AppDelegate ()块就新建一个):
    @interface AppDelegate ()
    @property (nonatomic, strong) MyAppPushNotificationsHandler *pnHandlerObj;
    @end
    
    然后在didFinishLaunchingWithOptions方法内初始化:
    self.pnHandlerObj = [[MyAppPushNotificationsHandler alloc] init];
    
    后续在AppDelegate的其他方法中,用self.pnHandlerObj调用即可避免未声明错误。

错误2:Use of undeclared identifier 'MyAppPushNotificationsHandler' / Unknown type name 'MyAppPushNotificationsHandler'

这是Swift类未正确暴露给Objective-C,或桥接配置出错导致的:

  • 检查Swift类的声明:必须添加@objc修饰符,且继承自NSObject,否则Objective-C无法识别:
    @objc class MyAppPushNotificationsHandler: NSObject {
        // 你的推送处理逻辑代码
    }
    
  • 核对Xcode桥接配置:
    1. 打开项目Build Settings,搜索Objective-C Generated Interface Header Name,确认值为app-Swift.h(和你导入的文件名完全一致)。
    2. app-Swift.h是Xcode自动生成的,不要手动创建该文件。
    3. 在AppDelegate.m中,#import "app-Swift.h"要放在所有自定义头文件的最后,避免循环引用。
  • 不要在@interface和@implementation之间声明变量,这个位置还未加载桥接头文件,必然无法识别Swift类。

额外验证步骤

  • 清理Xcode编译缓存:执行Product > Clean Build Folder,然后重新编译项目。
  • 确认Swift文件已添加到项目的target中,未被排除编译。

内容的提问来源于stack exchange,提问作者Amanda Dahlin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:10:33