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

Proximity sensor


In this recipe, we will discuss how to use the proximity sensor to disable the device screen.

Getting ready

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

Note

The simulator does not support the proximity sensor.

How to do it...

Perform the following steps:

  1. For this project, no controls are needed on the view controller. Declare an NSObject field that will hold the notification observer by using the following command:

    private NSObject proximityObserver;
  2. Override the ViewWillAppear method of the controller and implement it according to the following code:

    public override void ViewWillAppear (bool animated)
    {
      base.ViewWillAppear (animated);
      UIDevice.CurrentDevice.ProximityMonitoringEnabled = true;
      if (UIDevice.CurrentDevice.ProximityMonitoringEnabled)
      {
        this.proximityObserver = UIDevice.Notifications.ObserveProximityStateDidChange((s, e) => {
          Console.WriteLine("Proximity state: {0}", UIDevice.CurrentDevice.ProximityState)...