How to get static / relocatable framework name from class name in objective-c

337 Views Asked by At

I have a string with the name of a class and I'm trying to get the framework name where the class belongs.

Things that I have tried:

  1. bundleForClass
Class *myClass = NSClassFromString(@"ClassName");
[NSBundle bundleForClass:myClass];

But I can't seem to make it work with relocatable / static mach-o type.

  1. Adding new method on NSObject using categories
// This is on NSObject+ModuleID.h
@interface NSObject (ModuleID)
- (NSString *) moduleIdentifier;
@end

// This is on NSObject+ModuleID.m
@implementation NSObject (ModuleID)
- (NSString *) moduleIdentifier { return @"Framework Name"; }
@end

Class *myClass = NSClassFromString(@"ClassName");
[myClass moduleIdentifier];

This doesn't work since the project include more than 1 framework.

  1. Adding a new string property containing the framework name to each class in the frameworks
-(NSString *)moduleIdentifier
{ return @"Framework Name"; }

This is not feasible considering the number of the classes easily exceeds a hundred.

Is there any way to do this? Any input would be appreciated, thanks!

1

There are 1 best solutions below

2
Cy-4AH On

Try

-(NSString *)moduleIdentifier
{
    return [NSBundle bundleForClass:self.class].bundleIdentifier;
}