Google How to Disable / Enable the device sleep mode programmatically in Xamarin.iOS? | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Thursday 15 February 2018

How to Disable / Enable the device sleep mode programmatically in Xamarin.iOS?

Introduction:
When most apps have no touches as user input for a short period, the system puts the device into a "sleep” state where the screen dims. This is done for the purposes of conserving power. But in some time we may need to disable device sleep mode, this article can explain you about to disable/enable device sleep mode in iOS.

Requirements:
  • This article source code is prepared by using Visual Studio Community for Mac (7.3.2). And it is better to install latest visual studio updates from here.
  • This article is targeted for Xamarin.iOS and tested in iPhone 7 physical device.
Description:
Sometime, apps may need to prevent the sleeping mode to enhance user experience. For example, an app that play video would not go into sleep while the movie is playing. In IOS, a simple task can be done to prevent the app run into a sleep mode.

Note: Just remember to turn on the sleeping mode whenever the task finish. (for example, when your app stop playing video, you should turn the sleeping mode on again.

1. How to Prevent iOS app from device sleeping mode?
We can use the IdleTimerDisabled property that can controls whether the idle timer is disabled for the app. And default value of this property is false, so simply call below method in your AppDelegate source or ViewController to disable device sleep mode.

  1. public void DisableDeviceSleepMode()  
  2.        {  
  3.            InvokeOnMainThread(() => {  
  4.                UIApplication.SharedApplication.IdleTimerDisabled = true;  
  5.            });  
  6.        } 
2. How to enable iOS device sleeping mode?

  1. public void EnableDeviceSleepMode()  
  2.         {  
  3.             InvokeOnMainThread(() => {  
  4.                 UIApplication.SharedApplication.IdleTimerDisabled = false;  
  5.             });  
  6.         }  

Reference:


FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you?. It would be very happy, if you have any thoughts for to implement this requirement in any other way? I always welcome if you drop comments on this post and it would be impressive.

Follow me always at @Subramanyam_B
Have a nice day by  :)

No comments:

Post a Comment

Search Engine Submission - AddMe