Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Receiving notifications for app states


In this recipe, we will discuss how to get notified when the application's state changes outside the scope of the UIApplicationDelegate implementation.

Getting ready

Create a new Single View Application in Xamarin Studio and name it NotifyStatesApp.

How to do it...

Perform the following steps:

  1. Enter the following fields in the NotifyStatesAppViewController class:

    private NSObject appDidEnterBackgroundObserver, appWillEnterForegroundObserver;
  2. Create the following methods:

    private void AddNotificationObservers()
    {
      this.appDidEnterBackgroundObserver = UIApplication.Notifications.ObserveDidEnterBackground((s, e) => Console.WriteLine("App did enter background! App state: {0}", UIApplication.SharedApplication.ApplicationState));
      this.appWillEnterForegroundObserver = UIApplication.Notifications.ObserveWillEnterForeground((s, e) => Console.WriteLine("App will enter foreground! App state: {0}", UIApplication.SharedApplication.ApplicationState));
    }
    private...