Advertisement
aaaaaghghghghgh

AppDelegate

Jul 31st, 2024
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "AppDelegate.h"
  2.  
  3. #import "ReactNativeConfig.h"
  4. #import <React/RCTBundleURLProvider.h>
  5.  
  6. // BRAZE
  7. #import <BrazeKit/BrazeKit-Swift.h>
  8. #import "BrazeReactBridge.h"
  9. #import "BrazeReactUtils.h"
  10.  
  11.  
  12. // BOOT SPLASH SCREEN
  13. #import "RNBootSplash.h"
  14.  
  15. // EMBRACE
  16. #import <Embrace/Embrace.h>
  17.  
  18. // BRANCH
  19. #import <RNBranch/RNBranch.h>
  20.  
  21. // CHROMECAST
  22. #import <GoogleCast/GoogleCast.h>
  23.  
  24. // CODEPUSH
  25. #import <CodePush/CodePush.h>
  26.  
  27. // ORIENTATION
  28. #import "Orientation.h"
  29.  
  30. // CARPLAY
  31. #import <RNCarPlay.h>
  32.  
  33. // SDImage (WebP Support)
  34. #import "SDImageCodersManager.h"
  35. #import <SDWebImageWebPCoder/SDImageWebPCoder.h>
  36.  
  37. #import "CarScene.h"
  38. #import "MobileScene.h"
  39.  
  40.  
  41.  
  42. @implementation AppDelegate
  43.  
  44. #pragma mark - RCTAppDelegate
  45.  
  46. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  47.  
  48.   self.moduleName = @"wondery";
  49.   // You can add your custom initial props in the dictionary below.
  50.   // They will be passed down to the ViewController used by React Native.
  51.   self.initialProps = @{};
  52.  
  53.  
  54.   // CHROMECAST ************************************************************
  55.   NSString *receiverAppID = [ReactNativeConfig envFor:@"CHROMECAST_RECEIVER_ID"];
  56.   GCKDiscoveryCriteria *criteria = [[GCKDiscoveryCriteria alloc] initWithApplicationID:receiverAppID];
  57.   GCKCastOptions* options = [[GCKCastOptions alloc] initWithDiscoveryCriteria:criteria];
  58.   options.suspendSessionsWhenBackgrounded = false;
  59.   [GCKCastContext setSharedInstanceWithOptions:options];
  60.   // ************************************************************************
  61.  
  62.  
  63.  
  64.   // BRAZE *****************************************************************
  65.   NSString *brazeKey = [ReactNativeConfig envFor:@"BRAZE_KEY_IOS"];
  66.   NSString *brazeEndpoint = [ReactNativeConfig envFor:@"BRAZE_ENDPOINT"];
  67.  
  68.   BRZConfiguration *configuration = [[BRZConfiguration alloc] initWithApiKey:brazeKey
  69.                                                                     endpoint:brazeEndpoint];
  70.   Braze *braze = [BrazeReactBridge initBraze:configuration];
  71.   AppDelegate.braze = braze;
  72.  
  73.  
  74.   [application registerForRemoteNotifications];
  75.   UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
  76.   [center setNotificationCategories:BRZNotifications.categories];
  77.   center.delegate = self;
  78.   UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge | UNAuthorizationOptionProvisional;
  79.   [center requestAuthorizationWithOptions:authOptions
  80.                         completionHandler:^(BOOL granted, NSError *_Nullable error) {
  81.                           NSLog(@"Notification authorization, granted: %d, "
  82.                                 @"error: %@)",
  83.                                 granted, error);
  84.   }];
  85.  
  86.   [[BrazeReactUtils sharedInstance] populateInitialUrlFromLaunchOptions:launchOptions];
  87.   // ************************************************************************
  88.  
  89.   // BRANCH *****************************************************************
  90.   bool isProduction = [@"PROD" isEqualToString: [ReactNativeConfig envFor:@"ENV_NAME"]];
  91.   if (!isProduction) {
  92.     [RNBranch useTestInstance];
  93.   }
  94.   [RNBranch.branch checkPasteboardOnInstall];
  95.   [RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];
  96.   // *************************************************************************
  97.  
  98.   // EMBRACE *****************************************************************
  99.   NSString *embraceAppIdIOS = [ReactNativeConfig envFor:@"EMBRACE_APP_ID_IOS"];
  100.   [[Embrace sharedInstance] startWithKey:embraceAppIdIOS launchOptions: launchOptions framework:EMBAppFrameworkReactNative];
  101.   // ***********************************************************************
  102.  
  103.  
  104.   // PENTHERA **************************************************************
  105.   #ifdef DEBUG
  106.     // Development best practice
  107.     [VirtuosoLogger setLogLevel:kVL_LogVerbose];
  108.   #else
  109.     // Production best practice
  110.     [VirtuosoLogger setLogLevel:kVL_LogError];
  111.   #endif
  112.  
  113.   // ***********************************************************************
  114.  
  115.  
  116.   // Animated WEBP Support *************************************************
  117.   [SDImageCodersManager.sharedManager addCoder:SDImageWebPCoder.sharedCoder];
  118.   // ***********************************************************************
  119.  
  120.  
  121.   return [super application:application didFinishLaunchingWithOptions:launchOptions];
  122. }
  123.  
  124. - (UIViewController *)createRootViewController {
  125.  
  126.   UIViewController * vc = [[UIViewController alloc] init];
  127.   self.rootViewController = vc;
  128.   return vc;
  129.  
  130. }
  131.  
  132. - (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
  133.                           moduleName:(NSString *)moduleName
  134.                            initProps:(NSDictionary *)initProps {
  135.  
  136.   UIView *rootView = [super createRootViewWithBridge:bridge
  137.                                           moduleName:moduleName
  138.                                            initProps:initProps];
  139.  
  140.   if (@available(iOS 13.0, *)) {
  141.     self.bridge = bridge;
  142.     self.rootView = (RCTRootView*)rootView;
  143.   }
  144.  
  145.   [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; // ⬅️ initialize the splash screen
  146.   return rootView;
  147. }
  148.  
  149. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
  150. #if DEBUG
  151.   return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  152. #else
  153.   // return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  154.   // Replace above line with the following to enable CodePush. This change configures your app to always load the most recent version of your app's JS bundle.
  155.   // On the first launch, this corresponds to the file that was compiled with the app. However, after an update has been pushed via CodePush,
  156.   // this returns the location of the most recently installed update. NOTE: this will only use CodePush to resolve your JS bundle within RELEASE BUILDS.
  157.   return [CodePush bundleURL];
  158. #endif
  159. }
  160.  
  161. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  162. ///
  163. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  164. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  165. /// @return: `true` if the `concurrentRoot` feature is enabled. Otherwise, it returns `false`.
  166. - (BOOL)concurrentRootEnabled
  167. {
  168.   return true;
  169. }
  170.  
  171.  
  172. #pragma mark SceneDelegate
  173.  
  174. - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
  175.  
  176.   if([[connectingSceneSession role] isEqualToString:@"CPTemplateApplicationSceneSessionRoleApplication"]) {
  177.     UISceneConfiguration *scene = [[UISceneConfiguration alloc] initWithName:@"CarPlay" sessionRole:connectingSceneSession.role];
  178.     [scene setDelegateClass:CarSceneDelegate.class]; //TODO
  179.     return scene;
  180.   } else {
  181.     UISceneConfiguration *scene = [[UISceneConfiguration alloc] initWithName:@"Mobile" sessionRole:connectingSceneSession.role];
  182.     [scene setDelegateClass:MobileSceneDelegate.class]; //TODO
  183.     return scene;
  184.   }
  185. }
  186.  
  187. - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
  188.  
  189. }
  190.  
  191.  
  192. #pragma mark - AppDelegate.braze
  193.  
  194. static Braze *_braze = nil;
  195.  
  196. + (Braze *)braze {
  197.   return _braze;
  198. }
  199.  
  200. + (void)setBraze:(Braze *)braze {
  201.   _braze = braze;
  202. }
  203.  
  204. #pragma mark BRAZE Push
  205. - (void)registerForPushNotifications {
  206.   UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
  207.   [center setNotificationCategories:BRZNotifications.categories];
  208.   center.delegate = self;
  209.   [UIApplication.sharedApplication registerForRemoteNotifications];
  210.   // Authorization is requested later in the JavaScript layer via `Braze.requestPushPermission`.
  211. }
  212. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  213.   BOOL processedByBraze = AppDelegate.braze != nil && [AppDelegate.braze.notifications handleBackgroundNotificationWithUserInfo:userInfo
  214.                                                                                                         fetchCompletionHandler:completionHandler];
  215.   if (processedByBraze) {
  216.     return;
  217.   }
  218.  
  219.   completionHandler(UIBackgroundFetchResultNoData);
  220. }
  221.  
  222. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  223.   didReceiveNotificationResponse:(UNNotificationResponse *)response
  224.          withCompletionHandler:(void (^)(void))completionHandler {
  225.   BOOL processedByBraze = AppDelegate.braze != nil && [AppDelegate.braze.notifications handleUserNotificationWithResponse:response
  226.                                                                                                     withCompletionHandler:completionHandler];
  227.   if (processedByBraze) {
  228.     return;
  229.   }
  230.  
  231.   completionHandler();
  232. }
  233.  
  234.  
  235. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  236.        willPresentNotification:(UNNotification *)notification
  237.          withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
  238.   if (@available(iOS 14.0, *)) {
  239.     completionHandler(UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner);
  240.   } else {
  241.     completionHandler(UNNotificationPresentationOptionAlert);
  242.   }
  243. }
  244.  
  245. - (void)application:(UIApplication *)application
  246.   didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  247.   [AppDelegate.braze.notifications registerDeviceToken:deviceToken];
  248. }
  249.  
  250.  
  251. #pragma mark BRANCH react-native-branch
  252.  
  253. // react-native-branch AppDelegate calls moved above to SceneDelegate for iOS13+
  254. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  255.   if ([RNBranch application:app openURL:url options:options]) {
  256.     // do other deep link routing for the Facebook SDK, Pinterest SDK, etc
  257.   }
  258.   return YES;
  259. }
  260.  
  261. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
  262.   return [RNBranch continueUserActivity:userActivity];
  263. }
  264.  
  265. - (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity {
  266.   [RNBranch continueUserActivity:userActivity];
  267. }
  268.  
  269. - (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {
  270.   UIOpenURLContext *context = [URLContexts allObjects].firstObject;
  271.   if (context) {
  272.     NSMutableDictionary *options = [NSMutableDictionary dictionary];
  273.     options[UIApplicationOpenURLOptionsSourceApplicationKey] = context.options.sourceApplication;
  274.     options[UIApplicationOpenURLOptionsAnnotationKey] = context.options.annotation;
  275.     [RNBranch application:UIApplication.sharedApplication openURL:context.URL options:options];
  276.   }
  277. }
  278.  
  279. #pragma mark ORIENTATION
  280. - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  281.   return [Orientation getOrientation];
  282. }
  283.  
  284. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement