Skip to content

Instantly share code, notes, and snippets.

@valeIT
Forked from NSExceptional/ChatKitLoader.m
Created February 23, 2016 07:43
Show Gist options
  • Save valeIT/47a69685f48e97fe67eb to your computer and use it in GitHub Desktop.
Save valeIT/47a69685f48e97fe67eb to your computer and use it in GitHub Desktop.
A quick and dirty example of loading classes at runtime
#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