iOSAppDelegate.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /******************************************************************************/
  2. #include "iOS.h"
  3. /******************************************************************************/
  4. Bool DontRemoveThisOriOSAppDelegateClassWontBeLinked;
  5. /******************************************************************************/
  6. namespace EE{
  7. /******************************************************************************/
  8. void (*InitChartboostPtr)();
  9. /******************************************************************************/
  10. iOSAppDelegate* GetAppDelegate()
  11. {
  12. return (iOSAppDelegate*)[UIApplication sharedApplication].delegate;
  13. }
  14. /******************************************************************************/
  15. static void UpdateLocation(CLLocation *location, Bool gps)
  16. {
  17. if(location)
  18. if(location.horizontalAccuracy>=0) // can be negative if coordinates are invalid
  19. {
  20. LocationLat[gps]=location.coordinate.latitude;
  21. LocationLon[gps]=location.coordinate.longitude;
  22. LocationAlt[gps]=location.altitude;
  23. LocationSpd[gps]=Max(0, (Flt)location.speed); // can be negative if invalid
  24. LocationAcc[gps]=location.horizontalAccuracy;
  25. LocationTim[gps].from(location.timestamp);
  26. }
  27. }
  28. static void UpdateMagnetometer(CLHeading *heading)
  29. {
  30. if(heading)MagnetometerValue.set(heading.x, heading.y, -heading.z);
  31. }
  32. /******************************************************************************/
  33. } // namespace EE
  34. /******************************************************************************/
  35. @implementation iOSAppDelegate
  36. @synthesize window, controller;
  37. /******************************************************************************/
  38. -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  39. {
  40. ViewController=(MyViewController*)controller; // !! set this as first because other calls rely on this !!
  41. [window setRootViewController:controller]; // [window addSubview:controller.view];
  42. [window makeKeyAndVisible];
  43. // Chartboost
  44. if(InitChartboostPtr)InitChartboostPtr(); // call this from a pointer to function (and not directly to the advertisement codes) to avoid force linking to advertisement (which then links to chartboost lib, which uses 'advertisingIdentifier' which is prohibited when ads are not displayed in iOS, also linking to chartboost increases the app binary size)
  45. // Sensors
  46. REPA(LocationManager)if(!LocationManager[i])
  47. {
  48. LocationManager[i]=[[CLLocationManager alloc] init];
  49. LocationManager[i].delegate =self;
  50. LocationManager[i].desiredAccuracy=kCLLocationAccuracyBest;
  51. LocationManager[i].distanceFilter =kCLDistanceFilterNone;
  52. LocationManager[i]. headingFilter =kCLHeadingFilterNone;
  53. UpdateLocation (LocationManager[i ].location, i!=0);
  54. } if(LocationManager[true])UpdateMagnetometer(LocationManager[true].heading);
  55. // Facebook
  56. [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
  57. return true;
  58. }
  59. -(void)applicationWillResignActive:(UIApplication*)application
  60. {
  61. if(App._closed)return; // do nothing if app called 'Exit'
  62. App.setActive(false);
  63. }
  64. -(void)applicationDidBecomeActive:(UIApplication*)application
  65. {
  66. if(App._closed)return; // do nothing if app called 'Exit'
  67. App.setActive(true);
  68. // Facebook
  69. [FBSDKAppEvents activateApp];
  70. }
  71. -(void)applicationDidEnterBackground:(UIApplication*)application
  72. {
  73. if(App._closed)return; // do nothing if app called 'Exit'
  74. if(App.save_state)App.save_state();
  75. }
  76. -(void)applicationWillTerminate:(UIApplication*)application
  77. {
  78. if(App._closed)return; // do nothing if app called 'Exit'
  79. App.del();
  80. }
  81. -(void)applicationDidReceiveMemoryWarning:(UIApplication*)application
  82. {
  83. if(App._closed)return; // do nothing if app called 'Exit'
  84. App.lowMemory();
  85. }
  86. -(void)dealloc
  87. {
  88. [window release]; window =null;
  89. [controller release]; controller =null; ViewController=null;
  90. REPA(LocationManager){[LocationManager[i] release]; LocationManager[i]=null;}
  91. [super dealloc];
  92. }
  93. /******************************************************************************/
  94. -(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations
  95. {
  96. UpdateLocation([locations lastObject], manager==LocationManager[1]); // last is the newest
  97. }
  98. -(void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading
  99. {
  100. UpdateMagnetometer(newHeading);
  101. }
  102. /******************************************************************************
  103. // FACEBOOK
  104. /******************************************************************************/
  105. -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id>*)options
  106. {
  107. BOOL handled=[[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
  108. return handled;
  109. }
  110. -(void)sharer :(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary*)results {if(auto callback=FB.callback)callback(Facebook::POST_SUCCESS);} // copy first to temp var to avoid multi-threading issues
  111. -(void)sharer :(id<FBSDKSharing>)sharer didFailWithError :(NSError *)error {if(auto callback=FB.callback)callback(Facebook::POST_ERROR );} // copy first to temp var to avoid multi-threading issues
  112. -(void)sharerDidCancel:(id<FBSDKSharing>)sharer; {if(auto callback=FB.callback)callback(Facebook::POST_CANCEL );} // copy first to temp var to avoid multi-threading issues
  113. /******************************************************************************/
  114. @end
  115. /******************************************************************************/