From 0424e6bb884b0ff86af50f365dd2f32521194a7d Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Sun, 11 May 2014 11:41:54 +0100 Subject: [PATCH 1/7] Added an AppDelegate class, similar to the ActivityDelegate class found in the Android target. This makes it much easier for Module authors to hook into the application lifecycle, enabling deeper integration with the iOS platform. --- targets/ios/modules/native/iosgame.cpp | 32 +++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/targets/ios/modules/native/iosgame.cpp b/targets/ios/modules/native/iosgame.cpp index 445427fa..3931615c 100644 --- a/targets/ios/modules/native/iosgame.cpp +++ b/targets/ios/modules/native/iosgame.cpp @@ -1,6 +1,12 @@ //***** monkeygame.h ***** +class AppDelegate : public Object { +public: + virtual void applicationWillResignActive(UIApplication *application); + virtual void applicationDidBecomeActive(UIApplication *application); +} + class BBIosGame : public BBGame{ public: BBIosGame(); @@ -25,6 +31,10 @@ class BBIosGame : public BBGame{ virtual BBMonkeyAppDelegate *GetUIAppDelegate(); + virtual void AddAppDelegate(AppDelegate *appDelegate); + virtual void RemoveAppDelegate(AppDelegate *appDelegate); + virtual NSMutableArray *GetAppDelegates(); + //***** INTERNAL ***** virtual void StartGame(); @@ -42,6 +52,7 @@ class BBIosGame : public BBGame{ UIApplication *_app; BBMonkeyAppDelegate *_appDelegate; + NSMutableArray *_appDelegates; bool _displayLinkAvail; UIAccelerometer *_accelerometer; @@ -79,7 +90,8 @@ _displayLink( 0 ){ _app=[UIApplication sharedApplication]; _appDelegate=(BBMonkeyAppDelegate*)[_app delegate]; - + _appDelegates = [NSMutableArray arrayWithCapacity:1]; + NSString *reqSysVer=@"3.1"; NSString *currSysVer=[[UIDevice currentDevice] systemVersion]; if( [currSysVer compare:reqSysVer options:NSNumericSearch]!=NSOrderedAscending ) _displayLinkAvail=true; @@ -362,6 +374,18 @@ BBMonkeyAppDelegate *BBIosGame::GetUIAppDelegate(){ return _appDelegate; } +void BBIosGame::AddAppDelegate(AppDelegate *appDelegate) { + [_appDelegates addObject:appDelegate]; +} + +void BBIosGame::RemoveAppDelegate(AppDelegate *appDelegate) { + [_appDelegates removeObject:appDelegate]; +} + +NSMutableArray *BBIosGame::GetAppDelegates() { + return _appDelegates; +} + //***** INTERNAL ***** void BBIosGame::StartGame(){ @@ -704,11 +728,17 @@ void BBIosGame::ViewDisappeared(){ -(void)applicationWillResignActive:(UIApplication*)application{ game->SuspendGame(); + for (AppDelegate *appDelegate in game->GetAppDelegates()) { + appDelegate->applicationWillResignActive(application); + } } -(void)applicationDidBecomeActive:(UIApplication*)application{ game->ResumeGame(); + for (AppDelegate *appDelegate in game->GetAppDelegates()) { + appDelegate->applicationDidBecomeActive(application); + } } -(BOOL)textFieldShouldEndEditing:(UITextField*)textField{ From f6dca2bd919a09a77eb37fb475499eaf740fd26f Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Thu, 15 May 2014 11:16:55 +0100 Subject: [PATCH 2/7] - Renamed AppDelegate class to IosAppDelegate - Added additional app lifecycle methods - Fixed removal of app delegates using NSValue --- targets/ios/modules/native/iosgame.cpp | 60 ++++++++++++++++++-------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/targets/ios/modules/native/iosgame.cpp b/targets/ios/modules/native/iosgame.cpp index 3931615c..ee42ee97 100644 --- a/targets/ios/modules/native/iosgame.cpp +++ b/targets/ios/modules/native/iosgame.cpp @@ -1,11 +1,14 @@ //***** monkeygame.h ***** -class AppDelegate : public Object { +class IosAppDelegate : public Object { public: - virtual void applicationWillResignActive(UIApplication *application); - virtual void applicationDidBecomeActive(UIApplication *application); -} + virtual void applicationWillResignActive(UIApplication *application) {}; + virtual void applicationDidBecomeActive(UIApplication *application) {}; + virtual void applicationDidEnterBackground(UIApplication *application) {}; + virtual void applicationWillEnterForeground(UIApplication *application) {}; + virtual void applicationWillTerminate(UIApplication *application) {}; +}; class BBIosGame : public BBGame{ public: @@ -31,9 +34,9 @@ class BBIosGame : public BBGame{ virtual BBMonkeyAppDelegate *GetUIAppDelegate(); - virtual void AddAppDelegate(AppDelegate *appDelegate); - virtual void RemoveAppDelegate(AppDelegate *appDelegate); - virtual NSMutableArray *GetAppDelegates(); + virtual void AddIosAppDelegate(IosAppDelegate *appDelegate); + virtual void RemoveIosAppDelegate(IosAppDelegate *appDelegate); + virtual NSMutableArray *GetIosAppDelegates(); //***** INTERNAL ***** @@ -52,7 +55,7 @@ class BBIosGame : public BBGame{ UIApplication *_app; BBMonkeyAppDelegate *_appDelegate; - NSMutableArray *_appDelegates; + NSMutableArray *_iOSappDelegates; bool _displayLinkAvail; UIAccelerometer *_accelerometer; @@ -90,7 +93,7 @@ _displayLink( 0 ){ _app=[UIApplication sharedApplication]; _appDelegate=(BBMonkeyAppDelegate*)[_app delegate]; - _appDelegates = [NSMutableArray arrayWithCapacity:1]; + _iOSappDelegates = [[NSMutableArray array] retain]; NSString *reqSysVer=@"3.1"; NSString *currSysVer=[[UIDevice currentDevice] systemVersion]; @@ -374,16 +377,16 @@ BBMonkeyAppDelegate *BBIosGame::GetUIAppDelegate(){ return _appDelegate; } -void BBIosGame::AddAppDelegate(AppDelegate *appDelegate) { - [_appDelegates addObject:appDelegate]; +void BBIosGame::AddIosAppDelegate(IosAppDelegate *appDelegate) { + [_iOSappDelegates addObject:[NSValue valueWithPointer:appDelegate]]; } -void BBIosGame::RemoveAppDelegate(AppDelegate *appDelegate) { - [_appDelegates removeObject:appDelegate]; +void BBIosGame::RemoveIosAppDelegate(IosAppDelegate *appDelegate) { + [_iOSappDelegates removeObject:[NSValue valueWithPointer:appDelegate]]; } -NSMutableArray *BBIosGame::GetAppDelegates() { - return _appDelegates; +NSMutableArray *BBIosGame::GetIosAppDelegates() { + return _iOSappDelegates; } //***** INTERNAL ***** @@ -728,7 +731,8 @@ void BBIosGame::ViewDisappeared(){ -(void)applicationWillResignActive:(UIApplication*)application{ game->SuspendGame(); - for (AppDelegate *appDelegate in game->GetAppDelegates()) { + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; appDelegate->applicationWillResignActive(application); } } @@ -736,11 +740,33 @@ void BBIosGame::ViewDisappeared(){ -(void)applicationDidBecomeActive:(UIApplication*)application{ game->ResumeGame(); - for (AppDelegate *appDelegate in game->GetAppDelegates()) { + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; appDelegate->applicationDidBecomeActive(application); } } +-(void)applicationDidEnterBackground:(UIApplication*)application{ + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; + appDelegate->applicationDidEnterBackground(application); + } +} + +-(void)applicationWillEnterForeground:(UIApplication*)application{ + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; + appDelegate->applicationWillEnterForeground(application); + } +} + +-(void)applicationWillTerminate:(UIApplication*)application{ + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; + appDelegate->applicationWillTerminate(application); + } +} + -(BOOL)textFieldShouldEndEditing:(UITextField*)textField{ if( !textFieldState ) return YES; From 85d1d18ea6bd0182af6e23311b018e202b643903 Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Sat, 13 Sep 2014 12:36:40 +0100 Subject: [PATCH 3/7] IosAppDelegate no longer extends Object. --- targets/ios/modules/native/iosgame.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/ios/modules/native/iosgame.cpp b/targets/ios/modules/native/iosgame.cpp index ee42ee97..3d8ef5c3 100644 --- a/targets/ios/modules/native/iosgame.cpp +++ b/targets/ios/modules/native/iosgame.cpp @@ -1,7 +1,7 @@ //***** monkeygame.h ***** -class IosAppDelegate : public Object { +class IosAppDelegate { public: virtual void applicationWillResignActive(UIApplication *application) {}; virtual void applicationDidBecomeActive(UIApplication *application) {}; From 738f0f8a3e7c35f7e54399a712a3fd92231c80a2 Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Tue, 16 Sep 2014 20:50:28 +0100 Subject: [PATCH 4/7] Added an openURL method to the IosAppDelegate --- targets/ios/modules/native/iosgame.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/targets/ios/modules/native/iosgame.cpp b/targets/ios/modules/native/iosgame.cpp index 3d8ef5c3..030ef71b 100644 --- a/targets/ios/modules/native/iosgame.cpp +++ b/targets/ios/modules/native/iosgame.cpp @@ -8,6 +8,7 @@ class IosAppDelegate { virtual void applicationDidEnterBackground(UIApplication *application) {}; virtual void applicationWillEnterForeground(UIApplication *application) {}; virtual void applicationWillTerminate(UIApplication *application) {}; + virtual bool openURL(NSURL *url, NSString *sourceApplication) {}; }; class BBIosGame : public BBGame{ @@ -767,6 +768,15 @@ void BBIosGame::ViewDisappeared(){ } } +-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { + bool returnValue = NO; + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; + returnValue |= appDelegate->openURL(url, sourceApplication); + } + return returnValue; +} + -(BOOL)textFieldShouldEndEditing:(UITextField*)textField{ if( !textFieldState ) return YES; From 2166918367ec53d0155a3981fa75c2bab18cff9c Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Sun, 22 Feb 2015 14:39:35 +0000 Subject: [PATCH 5/7] Added a return value to the virtual method to remove the compiler warning. --- targets/ios/modules/native/iosgame.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/ios/modules/native/iosgame.cpp b/targets/ios/modules/native/iosgame.cpp index 030ef71b..d54e19d9 100644 --- a/targets/ios/modules/native/iosgame.cpp +++ b/targets/ios/modules/native/iosgame.cpp @@ -8,7 +8,7 @@ class IosAppDelegate { virtual void applicationDidEnterBackground(UIApplication *application) {}; virtual void applicationWillEnterForeground(UIApplication *application) {}; virtual void applicationWillTerminate(UIApplication *application) {}; - virtual bool openURL(NSURL *url, NSString *sourceApplication) {}; + virtual bool openURL(NSURL *url, NSString *sourceApplication) { return true; }; }; class BBIosGame : public BBGame{ From 7a48d50c7a530888e37868439c96718bb586948d Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Thu, 26 Feb 2015 13:14:07 +0000 Subject: [PATCH 6/7] Added support for Local Notifications by adding a 'didReceiveLocalNotification' method to the IosAppDelegate. This also involves keeping a reference to the last UILocalNotification received in 'didFinishLaunchingWithOptions' --- targets/ios/modules/native/iosgame.cpp | 13 +++++++++++++ targets/ios/modules/native/monkeytarget.cpp | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/targets/ios/modules/native/iosgame.cpp b/targets/ios/modules/native/iosgame.cpp index d54e19d9..b26b7670 100644 --- a/targets/ios/modules/native/iosgame.cpp +++ b/targets/ios/modules/native/iosgame.cpp @@ -9,6 +9,7 @@ class IosAppDelegate { virtual void applicationWillEnterForeground(UIApplication *application) {}; virtual void applicationWillTerminate(UIApplication *application) {}; virtual bool openURL(NSURL *url, NSString *sourceApplication) { return true; }; + virtual void didReceiveLocalNotification(UILocalNotification *notification) {}; }; class BBIosGame : public BBGame{ @@ -718,6 +719,10 @@ void BBIosGame::ViewDisappeared(){ return mask; } +-(NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window { + return UIInterfaceOrientationMaskAllButUpsideDown; +} + @end //***** BBMonkeyAppDelegate implementation ***** @@ -728,6 +733,7 @@ void BBIosGame::ViewDisappeared(){ @synthesize view; @synthesize viewController; @synthesize textField; +@synthesize localNotification; -(void)applicationWillResignActive:(UIApplication*)application{ @@ -777,6 +783,13 @@ void BBIosGame::ViewDisappeared(){ return returnValue; } +-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { + for (NSValue *appDelegateValue in game->GetIosAppDelegates()) { + IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue]; + appDelegate->didReceiveLocalNotification(notification); + } +} + -(BOOL)textFieldShouldEndEditing:(UITextField*)textField{ if( !textFieldState ) return YES; diff --git a/targets/ios/modules/native/monkeytarget.cpp b/targets/ios/modules/native/monkeytarget.cpp index e16c6ccd..37435a8b 100755 --- a/targets/ios/modules/native/monkeytarget.cpp +++ b/targets/ios/modules/native/monkeytarget.cpp @@ -39,6 +39,14 @@ class BBMonkeyGame : public BBIosGame{ [_window makeKeyAndVisible]; + Class cls = NSClassFromString(@"UILocalNotification"); + if (cls) { + UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; + if (notification) { + self.localNotification = notification; + } + } + return YES; } From 1b46b8ad34a9e82c190c8375fd3e1850968522ef Mon Sep 17 00:00:00 2001 From: Simon Jewell Date: Mon, 23 Mar 2015 20:24:09 +0000 Subject: [PATCH 7/7] Added missing definition of local notification object. --- targets/ios/template/main.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/targets/ios/template/main.h b/targets/ios/template/main.h index 7392685c..e68f54cc 100755 --- a/targets/ios/template/main.h +++ b/targets/ios/template/main.h @@ -95,6 +95,7 @@ class BBIosGame; MonkeyViewController *viewController; UITextField *textField; int textFieldState; + UILocalNotification *localNotification; } -(void)applicationWillResignActive:(UIApplication*)application; -(void)applicationDidBecomeActive:(UIApplication*)application; @@ -108,6 +109,7 @@ class BBIosGame; @property (nonatomic, retain) IBOutlet MonkeyView *view; @property (nonatomic, retain) IBOutlet MonkeyViewController *viewController; @property (nonatomic, retain) IBOutlet UITextField *textField; +@property (nonatomic, retain) IBOutlet UILocalNotification *localNotification; @end