Refresh a view from AppDelegate class

Hi Everyone.
Could someone help me with and issue if it is possible. In my iOS app I have registered a URL type in the info.plist file, so if I click a specific url on the phone it will launch my app. This is working, What I am doing is when the url is clicked and the app is launched, I do some server comms in the public bool OpenUrl method which adds an item to my app. I need to refresh the view controller to display that item now.
Currently I have to kill the app and relaunch it, then I can see the item.

Is there a way to get an instance of the view controller in the AppDelegate.cs class and do a refresh or even just to get an instance so that I can call a method in the View Controller from the AppDelegate class?

Any help will be appreciated. I have been searching online, but have not found any suitable solution yet.

Thank you

Regards,

Answers

  • It is possible to get the visible view controller from the AppDelegate.


    var window= UIApplication.SharedApplication.KeyWindow;
    var vc = window.RootViewController;
    If (vc.PresentedViewController != null)
    {
    vc = vc.PresentedViewController;
    }

    You can also add an event to the AppDelegate that your view controller can subscribe to and update UI accordingly.
  • CollindeWittCollindeWitt USMember ✭✭

    @JasonFeinkind
    Thank you, I managed to call methods in my ViewController using the following:

    this is in my override bool OpenUrl method in appdelegate.

    UIViewController root = this.Window.RootViewController;
    ViewController vc = (ViewController)root.ChildViewControllers[0];
    vc.MyMethod(url.ToString());
    

    In my View controller I handled refreshing after new dynamic data came back from server.

Sign In or Register to comment.