Allow overriding the app delegate on iOS
authorSam Lantinga <slouken@libsdl.org>
Tue, 05 Apr 2011 09:50:25 -0700
changeset 55328a04b596b472
parent 5531 477f550f6efd
child 5533 7cf203db63f6
Allow overriding the app delegate on iOS

Vittorio G. to Eric, Sam

Actually this is much simpler than i thought, I just had to specify a
class method to get the delegate name and then the category can
override that method!
I've attached the patch that enables this features: in my code i could
remove my custom main() and simply add

@implementation SDLUIKitDelegate (customDelegate)
+(NSString *)getAppDelegateClassName {
return @"HedgewarsAppDelegate";
}
@end

I tested it and with the sdl demos it loads the normal
SDLUIKitDelegate, while in my code it loads my HedgewarsAppDelegate!
src/video/uikit/SDL_uikitappdelegate.h
src/video/uikit/SDL_uikitappdelegate.m
     1.1 --- a/src/video/uikit/SDL_uikitappdelegate.h	Tue Apr 05 09:47:34 2011 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitappdelegate.h	Tue Apr 05 09:50:25 2011 -0700
     1.3 @@ -28,6 +28,7 @@
     1.4  }
     1.5  
     1.6  +(SDLUIKitDelegate *)sharedAppDelegate;
     1.7 ++(NSString *)getAppDelegateClassName;
     1.8  
     1.9  @end
    1.10  /* *INDENT-ON* */
     2.1 --- a/src/video/uikit/SDL_uikitappdelegate.m	Tue Apr 05 09:47:34 2011 -0700
     2.2 +++ b/src/video/uikit/SDL_uikitappdelegate.m	Tue Apr 05 09:50:25 2011 -0700
     2.3 @@ -50,7 +50,7 @@
     2.4      forward_argv[i] = NULL;
     2.5  
     2.6      /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
     2.7 -    UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
     2.8 +    UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
     2.9      
    2.10      [pool release];
    2.11      return 0;
    2.12 @@ -64,6 +64,12 @@
    2.13      return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    2.14  }
    2.15  
    2.16 ++(NSString *)getAppDelegateClassName {
    2.17 +    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
    2.18 +       this method and return the actual name of the delegate */
    2.19 +    return @"SDLUIKitDelegate";
    2.20 +}
    2.21 +
    2.22  - (id)init {
    2.23      self = [super init];
    2.24      return self;
    2.25 @@ -90,8 +96,7 @@
    2.26      /* Set working directory to resource path */
    2.27      [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
    2.28      
    2.29 -    [self performSelector:@selector(postFinishLaunch) withObject:nil
    2.30 -afterDelay:0.0];
    2.31 +    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
    2.32  
    2.33      return YES;
    2.34  }
    2.35 @@ -113,7 +118,7 @@
    2.36          return;
    2.37      }
    2.38  	
    2.39 -	SDL_Window *window;
    2.40 +    SDL_Window *window;
    2.41      for (window = _this->windows; window != nil; window = window->next) {
    2.42          SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
    2.43      }