Skip to content

Dynamic class name in %init(<class>=<expr>) and %hook <class> to assume type #43

@Nosskirneh

Description

@Nosskirneh

TLDR

Specifying the class like %init([<class>=<expr>, …]);, results in the hooked object (self) being left without type to the compiler. I understand that not assuming any class is the most sane thing to do, but I was wondering if it would be possible to automatically set the object to a known type or to one that conforms to a protocol in order to avoid casting.

Background

I have a few hooks for iOS 11 and 12 (pre 12.2) that hooks the class MediaControlsPanelViewController in a few of my tweaks. In iOS 12.2 however, this class was renamed to MRPlatterViewController. All properties and methods seems to be the same. Thus, I would like to reuse the code within the hook, like:

%ctor {
    ...

    Class c;
    if (running iOS 12.2)
        c = %c(MRPlatterViewController);
    else
        c = %c(MediaControlsPanelViewController);
    %init(MediaControlsPanelViewController = c);
}

This works fine, but inside the actual hooked code, the compiler does no longer know which type self has.

I realize that it's possible to do something like this:

@protocol PanelViewController<NSObject>
@property (nonatomic, retain) MediaControlsParentContainerView *parentContainerView;
...
@end

@interface MediaControlsPanelViewController : UIViewController <PanelViewController>
@end

@interface MRPlatterViewController : UIViewController <PanelViewController>
@end

and then in the code cast it to an object that conforms to that protocol:

%hook MediaControlsPanelViewController

- (void)viewDidLoad {
    %orig;

    UIViewController<PanelViewController> *controller = (UIViewController<PanelViewController> *)self;

    controller.parentContainerView...
}
...

instead of using self. I was wondering if it would be possible to automatically set the object to a known type or to one that conforms to a protocol in order to avoid doing this casting. Ideally this would be done in the %ctor once instead of once in every hooked method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions