Skip to content

Instantly share code, notes, and snippets.

@mikehouse
Last active September 21, 2018 15:34
Show Gist options
  • Save mikehouse/bbf1a3edf0a836b350bc0d53c806a23b to your computer and use it in GitHub Desktop.
Save mikehouse/bbf1a3edf0a836b350bc0d53c806a23b to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface ApplicationDelegateProxyType ()
@property (nonnull, nonatomic) NSArray<id <UIApplicationDelegate>> *objects;
@end
@implementation ApplicationDelegateProxyType
static NSArray<id <UIApplicationDelegate>> *(^_provider)(void) = nil;
- (instancetype)init {
_objects = [_provider() copy];
return self;
}
+ (void)setDelegateProvider:(NSArray<id <UIApplicationDelegate>> * (^)(void))provider {
_provider = provider;
}
- (BOOL)respondsToSelector:(SEL)aSelector {
for (id <UIApplicationDelegate> delegate in self.objects) {
if ([delegate respondsToSelector:aSelector]) {
return YES;
}
}
return NO;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL sel = invocation.selector;
for (id <UIApplicationDelegate> delegate in self.objects) {
if ([delegate respondsToSelector:sel]) {
[invocation invokeWithTarget:delegate];
}
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
for (NSObject *delegate in self.objects) {
if ([delegate respondsToSelector:sel]) {
return [delegate methodSignatureForSelector:sel];
}
}
return nil;
}
@end
// main.swift
ApplicationDelegateProxyType.setDelegateProvider { () -> [UIApplicationDelegate]? in
return [
MainDelegate(),
AnalyticsDelegate()
]
}
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
NSStringFromClass(UIApplication.self),
NSStringFromClass(ApplicationDelegateProxyType.self)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment