transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

AppDelegate.swift example #446

Closed defiantgoat closed 2 months ago

defiantgoat commented 1 year ago

We have migrated to an AppDelegate.swift model with a bridging header. Would be good to provide a swift example for this part > https://github.com/transistorsoft/react-native-background-fetch/blob/master/docs/INSTALL-AUTO-IOS.md#appdelegatem-new-ios-13

defiantgoat commented 1 year ago

cc @luketgriffith

raid5 commented 2 months ago

Same issue here, it would be nice to be able to call directly instead of calling into an ObjC class

christocracy commented 2 months ago
import UIKit
import Flutter
+import TSBackgroundFetch

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      .
      .
      .    
+   TSBackgroundFetch.sharedInstance().didFinishLaunching();

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
christocracy commented 2 months ago

Updated Setup Instructions

I don't think it's a good idea to use Swift with the AppDelegate, until RN provides a built-in way to generate one. And when RN changes the source of their AppDelegate.m in the future (and they will), then you're screwed and you have to manually translate the changes to Swift. Stackoverflow is full of many different versions of AppDelegeate.swift through various versions of RN dating back years.

:open_file_folder: AppDelegate.swift:

import Foundation
import UIKit
+import TSBackgroundFetch

@UIApplicationMain
class AppDelegate: RCTAppDelegate {

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    .
    .
    .   
+   // [REQUIRED] Register BackgroundFetch
+   TSBackgroundFetch.sharedInstance().didFinishLaunching();
    return self.application(application, didFinishLaunchingWithOptions: launchOptions);
  }
}