Skip to content

Refactor ForEachAppDelegateClass to use standard iOS/tvOS techniques #1736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions app/src/util_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,48 @@ - (BOOL)application:(UIApplication *)application
namespace util {

void ForEachAppDelegateClass(void (^block)(Class)) {
unsigned int number_of_classes;
Class *classes = objc_copyClassList(&number_of_classes);
for (unsigned int i = 0; i < number_of_classes; i++) {
Class clazz = classes[i];
if (class_conformsToProtocol(clazz, @protocol(UIApplicationDelegate))) {
const char *class_name = class_getName(clazz);
bool blacklisted = false;
static const char *kClassNameBlacklist[] = {
// Declared in Firebase Analytics:
// //googlemac/iPhone/Firebase/Analytics/Sources/ApplicationDelegate/
// FIRAAppDelegateProxy.m
"FIRAAppDelegate",
// Declared here.
"FIRSAMAppDelegate"};
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kClassNameBlacklist); ++i) {
if (strcmp(class_name, kClassNameBlacklist[i]) == 0) {
blacklisted = true;
break;
}
// Get the application delegate class directly.
// This assumes that the application has a single app delegate, which is the
// standard practice for iOS/tvOS apps.
// This needs to be called at or after +load time, when the application delegate is set.
Class appDelegateClass = nil;
UIApplication *sharedApplication = [UIApplication sharedApplication];
if (sharedApplication) {
id<UIApplicationDelegate> appDelegate = sharedApplication.delegate;
if (appDelegate) {
appDelegateClass = [appDelegate class];
}
}

if (appDelegateClass && class_conformsToProtocol(appDelegateClass, @protocol(UIApplicationDelegate))) {
const char *class_name = class_getName(appDelegateClass);
bool blacklisted = false;
static const char *kClassNameBlacklist[] = {
// Declared in Firebase Analytics:
// //googlemac/iPhone/Firebase/Analytics/Sources/ApplicationDelegate/
// FIRAAppDelegateProxy.m
"FIRAAppDelegate",
// Declared here.
"FIRSAMAppDelegate"};
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kClassNameBlacklist); ++i) {
if (strcmp(class_name, kClassNameBlacklist[i]) == 0) {
blacklisted = true;
break;
}
if (!blacklisted) {
if (GetLogLevel() <= kLogLevelDebug) {
// Call NSLog directly because we may be in a +load method,
// and C++ classes may not be constructed yet.
NSLog(@"Firebase: Found UIApplicationDelegate class %s", class_name);
}
block(clazz);
}
if (!blacklisted) {
if (GetLogLevel() <= kLogLevelDebug) {
// Call NSLog directly because we may be in a +load method,
// and C++ classes may not be constructed yet.
NSLog(@"Firebase: Using UIApplicationDelegate class %s", class_name);
}
block(appDelegateClass);
}
} else {
if (GetLogLevel() <= kLogLevelWarning) {
NSLog(@"Firebase: Could not find a valid UIApplicationDelegate class.");
}
}
free(classes);
}

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