Last active
January 15, 2017 17:46
-
-
Save NSExceptional/6e468f5a1b6401592524 to your computer and use it in GitHub Desktop.
A quick and dirty example of loading classes at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <dlfcn.h> // for dlopen() | |
#import "MirrorKit.h" // pod 'MirrirKit' | |
- (NSArray<NSString *> *)chatKitClassNames { | |
// Load the framework, close the handle. Now you can use any ChatKit class if you know its name. | |
void *handle = dlopen("/System/Library/PrivateFrameworks/ChatKit.framework/ChatKit", RTLD_NOW); | |
dlclose(handle); | |
// Making use of the Objc runtime to get the classes | |
NSArray *classes = [NSObject allSubclasses]; | |
classes = [classes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id class, NSDictionary *bindings) { | |
return [NSStringFromClass(class) hasPrefix:@"CK"]; | |
}]]; | |
NSMutableArray *names = [NSMutableArray array]; | |
for (Class cls in classes) | |
[names addObject:NSStringFromClass(cls)]; | |
return names; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment