Skip to content

Commit 1986ab7

Browse files
committed
Use objc_enumerateclasses on iOS 16+.
1 parent 7a62b09 commit 1986ab7

File tree

1 file changed

+54
-28
lines changed

1 file changed

+54
-28
lines changed

app/src/util_ios.mm

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,63 @@ - (BOOL)application:(UIApplication *)application
8080
namespace util {
8181

8282
void ForEachAppDelegateClass(void (^block)(Class)) {
83-
unsigned int number_of_classes;
84-
Class *classes = objc_copyRealizedClassList(&number_of_classes);
85-
for (unsigned int i = 0; i < number_of_classes; i++) {
86-
Class clazz = classes[i];
87-
if (class_conformsToProtocol(clazz, @protocol(UIApplicationDelegate))) {
88-
const char *class_name = class_getName(clazz);
89-
bool blacklisted = false;
90-
static const char *kClassNameBlacklist[] = {
91-
// Declared in Firebase Analytics:
92-
// //googlemac/iPhone/Firebase/Analytics/Sources/ApplicationDelegate/
93-
// FIRAAppDelegateProxy.m
94-
"FIRAAppDelegate",
95-
// Declared here.
96-
"FIRSAMAppDelegate"};
97-
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kClassNameBlacklist); ++i) {
98-
if (strcmp(class_name, kClassNameBlacklist[i]) == 0) {
99-
blacklisted = true;
100-
break;
101-
}
102-
}
103-
if (!blacklisted) {
104-
if (GetLogLevel() <= kLogLevelDebug) {
105-
// Call NSLog directly because we may be in a +load method,
106-
// and C++ classes may not be constructed yet.
107-
NSLog(@"Firebase: Found UIApplicationDelegate class %s", class_name);
108-
}
109-
block(clazz);
83+
static const char *kClassNameBlacklist[] = {
84+
// Declared in Firebase Analytics:
85+
// //googlemac/iPhone/Firebase/Analytics/Sources/ApplicationDelegate/
86+
// FIRAAppDelegateProxy.m
87+
"FIRAAppDelegate",
88+
// Declared here.
89+
"FIRSAMAppDelegate"};
90+
if (@available(iOS 16, *)) {
91+
// Use objc_enumerateClasses on iOS 16 and later
92+
objc_enumerateClasses(/* image = */ NULL,
93+
/* namePrefix = */ NULL,
94+
/* conformingTo = */ @protocol(UIApplicationDelegate),
95+
/* subclassing = */ NULL,
96+
/* block = */ ^(Class clazz, BOOL * /* stop */) {
97+
const char *class_name = class_getName(clazz);
98+
bool blacklisted = false;
99+
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kClassNameBlacklist); ++i) {
100+
if (strcmp(class_name, kClassNameBlacklist[i]) == 0) {
101+
blacklisted = true;
102+
break;
103+
}
104+
}
105+
if (!blacklisted) {
106+
if (GetLogLevel() <= kLogLevelDebug) {
107+
// Call NSLog directly because we may be in a +load method,
108+
// and C++ classes may not be constructed yet.
109+
NSLog(@"Firebase: Found UIApplicationDelegate class %s", class_name);
110+
}
111+
block(clazz);
112+
}
113+
});
114+
} else { // iOS 15 or earlier
115+
unsigned int number_of_classes;
116+
Class *classes = objc_copyClassList(&number_of_classes);
117+
for (unsigned int i = 0; i < number_of_classes; i++) {
118+
Class clazz = classes[i];
119+
if (class_conformsToProtocol(clazz, @protocol(UIApplicationDelegate))) {
120+
const char *class_name = class_getName(clazz);
121+
bool blacklisted = false;
122+
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kClassNameBlacklist); ++i) {
123+
if (strcmp(class_name, kClassNameBlacklist[i]) == 0) {
124+
blacklisted = true;
125+
break;
126+
}
127+
}
128+
if (!blacklisted) {
129+
if (GetLogLevel() <= kLogLevelDebug) {
130+
// Call NSLog directly because we may be in a +load method,
131+
// and C++ classes may not be constructed yet.
132+
NSLog(@"Firebase: Found UIApplicationDelegate class %s", class_name);
133+
}
134+
block(clazz);
135+
}
110136
}
111137
}
138+
free(classes);
112139
}
113-
free(classes);
114140
}
115141

116142
NSDictionary *StringMapToNSDictionary(const std::map<std::string, std::string> &string_map) {

0 commit comments

Comments
 (0)