Accessing AppState in AppDelegate with SwiftUI’s new iOS 14 life cycle

Use shared instance for AppState class AppState: ObservableObject { static let shared = AppState() // << here !! // Singe source of truth… @Published var user = User() } so you can use it everywhere struct MyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @StateObject var appState = AppState.shared // … other code } and func application(_ … Read more

How to get managedObjectContext for viewController other than getting it from appDelegate?

It is called dependency injection. Basically the caller/constructor should be setting the NSManagedObjectContext onto the called/constructed. In your AppDelegate you should set the NSManagedObjectContext into the rootViewController that is associated with the UIWindow. Your rootViewController should then set the NSManagedObjectContext into the next view controller and so on. How? It is just a simple proper … Read more

Present UIAlertController from AppDelegate [duplicate]

You can use this code as well if you want to launch it from didFinishLaunchingWithOptions.Hope this helps. dispatch_async(dispatch_get_main_queue(), { let importantAlert: UIAlertController = UIAlertController(title: “Action Sheet”, message: “Hello I was presented from appdelegate ;)”, preferredStyle: .ActionSheet) self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil) }) And up-to-date: DispatchQueue.main.async { let alert = UIAlertController(title: “Hello!”, message: “Greetings from AppDelegate.”, … Read more

What describes the Application Delegate best? How does it fit into the whole concept?

In Cocoa, a delegate is an object that another object defers to on questions of behavior and informs about changes in its state. For instance, a UITableViewDelegate is responsible for answering questions about how the UITableView should behave when selections are made or rows are reordered. It is the object that the UITableView asks when … Read more

Open a view controller when a iOS push notification is received

You may be having issues with the if (applicationIsActive) condition. Put a breakpoint on -didReceiveRemoteNotification and see whether it executes in different scenarios and see if it goes within the if-condition. (unrelated to a certain extent but worth checking) this question: didReceiveRemoteNotification when in background Note: -didReceiveRemoteNotification will not execute if your app was (initially) … Read more

AppDelegate, rootViewController and presentViewController

I had the same issue. Based on the answer to this question, I added [self.window makeKeyAndVisible] just before presentViewController:animated:completion:, and that fixed it for me. In your case, showLoginView becomes – (void)showLoginView { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@”MainStoryboard” bundle:nil]; LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:@”LoginViewController”]; [self.window makeKeyAndVisible]; [self.window.rootViewController presentViewController:loginViewController animated:YES completion:NULL]; }