iOS中Swizzling状态查询:集成FCM通知时如何判断启用/禁用?
如何查询FCM通知的Swizzling方法状态?
嘿,这个问题我之前在集成FCM的时候也纠结过,分享几个实用的检查方式:
1. 检查Info.plist配置
FCM的Swizzling状态最直接的控制开关就在Info.plist里,你可以找一下这个键:FirebaseAppDelegateProxyEnabled
- 如果值设为
YES(或布尔类型的YES),说明Swizzling处于启用状态 - 如果值设为
NO,就是禁用状态 - 要是这个键压根没在Info.plist里,那默认就是启用Swizzling的
2. 查看代码中的手动配置痕迹
如果项目里手动接管了FCM的通知回调,那大概率是关闭了Swizzling。你可以检查以下几个点:
- 有没有在
AppDelegate或者UNUserNotificationCenterDelegate的实现里,手动处理了这些核心方法:application:didReceiveRemoteNotification:fetchCompletionHandler:userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:userNotificationCenter:willPresentNotification:withCompletionHandler:
- 初始化Firebase时,有没有补充过禁用Swizzling的配置(通常这个会和Info.plist的开关配合使用)
3. 运行时验证(快速确认)
如果上面的方法还不确定,可以写个小代码片段在运行时验证:
在AppDelegate的application:didFinishLaunchingWithOptions:里加一段代码:
OC代码示例
#import <objc/runtime.h> // 检查application:didReceiveRemoteNotification:fetchCompletionHandler:是否被Swizzle Method originalMethod = class_getInstanceMethod([UIApplication class], @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)); Method currentMethod = class_getInstanceMethod([AppDelegate class], @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)); if (originalMethod != currentMethod) { NSLog(@"Swizzling已启用:方法实现被替换"); } else { NSLog(@"Swizzling已禁用:使用原始方法实现"); }
Swift代码示例
import ObjectiveC let targetSelector = #selector(UIApplicationDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:)) if let originalMethod = class_getInstanceMethod(UIApplicationDelegate.self, targetSelector), let currentMethod = class_getInstanceMethod(AppDelegate.self, targetSelector) { if originalMethod != currentMethod { print("Swizzling已启用:方法实现被替换") } else { print("Swizzling已禁用:使用原始方法实现") } }
运行App后看控制台输出,就能快速判断状态了。
内容的提问来源于stack exchange,提问作者UseriOSDev
相关产品推荐
相关产品推荐

