Question

I have code inheriting from UIActivity introduced in iOS 6 which compiles fines with Xcode 4.5 and works fine on iOS 6 and previous versions (I detect availability of the class at runtime). However, is it possible to make this code compile with Xcode 4.4, which does not include the UIActivity class in its SDK?

If I forward declare UIActivity the dealloc method doesn't compile because it calls super and the compiler warns me the class is already at the root of the inheritance tree. Maybe there is a way to make this class inherit from a proxy class which I define locally, and then at runtime somehow swizzle it and instantiate it as if it had been properly defined at compile time? The purpose of this is to compile code with Xcode 4.4 and have the binary run on iOS 6 using that phantom class.

I know I can use defines to prevent my subclass from compiling at all with Xcode 4.4 and previous, but that would mean the functionality won't be available on a device running iOS 6.

Was it helpful?

Solution

@interface UIActivityDummy : NSObject
//Copy UIActivity methods to avoid compiler warnings
@end

@implementation UIActivityDummy
@end

@interface MyClass : UIActivityDummy
@end

@implementation MyClass

+ (void) initialize {
    Class activityClass = objc_getClass("UIActivity");
    if (activityClass) {
        class_setSuperclass([MyClass class], activityClass);
    }
    NSLog(@"%@", [[MyClass class] superclass]);
}

@end

OTHER TIPS

Here, I just elaborating how we can get the class at run-rime. Hope it'll what you needed .Please check it.

just use:

objc_lookUpClass(myClass);// it check for the required class. objc_getClass(myClass);//IT's what you needed.

Please check the Syntax, as I'm not sure. But, once a time I had used this and was also able to get the ivar and methods and was also able to add method at runtime.

In any concern get back to me. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top