Skip to content

Instantly share code, notes, and snippets.

@Valpertui
Forked from NSExceptional/ChatKitLoader.m
Created February 20, 2016 10:37
Show Gist options
  • Save Valpertui/54ee87f35c3023b503f4 to your computer and use it in GitHub Desktop.
Save Valpertui/54ee87f35c3023b503f4 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