Calling Delegate Function from Appdelegate Not Working

Calling delegate function from AppDelegate not working

You cannot create the Viewcontroller object directly and set the delegate inside your app delegate. You need to access the Viewcontroller object first because it is the inside the rootViewController so you need to implement like this

if let navigationController = window?.rootViewController, let 
viewController = navigationController.childViewControllers.first as? ViewController {
viewController.delegate = self
}

Functions of AppDelegate not being called

If you are using ios 13 with scene delegate .. all functions are shifted in scene delegate instead of AppDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}

func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}

func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}

func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}

func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}

func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}

}

Delegate function not called in AppDelegate

AppDelegate being a singleton need not have the ApplicationTiomeoutDelegate protocol invocation. You can directly invoke addObserverForTimeout

How to call method in view controller in app delegate?

if its the root its easy:

(window.rootViewController as? ViewController)?.myFunction()

if its on top of a navigationController

((window.rootViewController as? UINavigationController)?.topViewController as? ViewController)?.myFunction()

But in either case its weird and probably not the right thing to do. Use NotificationCenter instead and post a notification name in your AppDelegate and then have the ViewController listen for messages and respond accordingly.

Call app delegate method from view controller

Not sure why you want to do this. You probably shouldn't, but for the purpose of answering the question here it is:

// get a reference to the app delegate
let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate

// call didFinishLaunchWithOptions ... why?
appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)

Xcode; How to call function in AppDelegate from another ViewController

Try something along those lines:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")

EDIT:
'Today Extension' and App live in a different contexts so it is not possible.

Why is UIApplicationDelegate method `application(_:configurationForConnecting:options:)` not called reliably

This seems to be expected behaviour, and makes sense once you understand what's going on, but it's not documented. I've just spent some fairly traumatic time getting to the bottom of it. Oh, Apple.

The key thing to know is that when you relaunch an app, the windows from the previous run are restored.

(It also helps to remember that an app can have multiple types of window – each represented by a scene configuration – which is why you might implement this delegate method in the first place.)

Case 1: App launched for the first time ever

The app doesn't know what type of scene to put in the window, and calls application(_:configurationForConnecting:options:) to find out. So far things are as we expect. (If you don't implement this delegate method, it just falls back to the first suitable entry in your Info.plist's scene manifest, if it has one.)

Case 2: New window created (for apps that support multiple windows)

(e.g. by dragging the dock icon on iPad). The app doesn't know what to put in this window either. Same as case 1.

Case 3: App relaunched

The OS wants to restore your windows. To do this, it has remembered the scene configs of the windows you had open last time. Surprise! It knows what scenes to put in the windows, and doesn't ask your app delegate. It just goes ahead and creates the scenes using the remembered configs.

For the poor developer thinking in terms of a window being created when the app starts up, this is confusing. But if you think in terms of windows being restored at startup, not created - even when there is only one - it starts to make sense.


Now, if you want to reset things so your windows are forgotten and your delegate method is called on next launch:

  • for iOS, delete the app
  • for Catalyst, delete the app's container

Note 1: In Catalyst, it seems that only the first window is restored on relaunch, but otherwise behaviour is the same as above. Have now observed this not to be true. Perhaps it's inconsistent.

Note 2: You can also restore your windows' content, not just their type, using UIWindowSceneDelegate and UISceneSession.stateRestorationActivity, but that's another story.

iOS - Calling SceneDelegate method from ViewController

UIApplication.shared.delegate is AppDelegate not SceneDelegate You need

if let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {    
// to do
}


Related Topics



Leave a reply



Submit