How to turn Auto-Lock to Never in Swift? - swift

Is there any Swift command to actually set the Auto-Lock to Never or a specific time period? I want to create a simple app that only has two buttons: one is to set the Auto-Lock to Never and the other one is to set it back to iOS default (1 min).
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps. If s/he is done with other apps, s/he can open this app again and tap the Default button to set the Auto-Lock back to 1 min.
I understand this can be done from the Settings but I am just curious how I can do it from the backend using Swift.
I am new to Swift, btw.
Thanks much!

So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps
You can't do that. You are sandboxed. You cannot affect what happens to the user while running some other app.

When app is open, try to disable the idleTimer in viewDidLoad
UIApplication.shared.isIdleTimerDisabled = true
When app is closed to open other apps, try to -enable again idleTimer when yourViewController disappears, so put this in viewDidDisappear
UIApplication.shared.isIdleTimerDisabled = true

You need to make use of this API call to set the idle timer disable and enable.
This is in objective-C. Just convert it to swift. The API is available in UIApplication.h
-(void) onApplicationDidActivate:(NSNotification*) notification
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
-(void) onApplicationWillDeactivate
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Related

Lock iphone home screen from my app

I want to lock the device home screen programatically from my app. Is it possible in IOS?
In my app, if the user is idle for 1 minute, i want to lock the device. This is the scenario.
Thanks
Jithen
you can lock the phone using bellow code
[UIApplication sharedApplication].idleTimerDisabled = YES;
You can lock your own App by Adding a block view on Appdelegate's window. But locking your device is the responsibility of OS. I think if you somehow implement this then still aplle might reject your app on it.

iOS app badge persists

I have an iPhone app which uses local notification. It is working almost perfectly, although I have a little problem: The red balloon (app badge) from notification doesn't dismiss when I launch the app. What can I do to fix it?
I've already deleted the app from iPhone, but when I compile it again with Xcode, I comes back again.
Try
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
after you have processed your notification.

Relaunch application when the screen is unlocked

When the application is on and when we leave it to idle, the screen gets lock. When the user unlocks it (slides), I need to relaunch the application.
For that i used the following code in the applicationDidFinishLaunching method.
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
Still the application doesn't relaunch after the screen is unlocked. Help, what should i do to make this work ?
You do not have to do anything for that. If an application is open before locking the iphone, it will be open, if you unlock it again.
setIdleTimerDisabled just ensures, that the device won't go to sleep on its own, while your application is running. In most cases you shouldn't use this application-wide.

How do I keep the user's iPhone's display on?

So, I was looking for a way to keep the user's iPhone display on for a clock app. I found [UIApplication sharedApplication].idleTimerDisabled = YES; but that keeps the device from locking all of the time. I tried to [UIApplication sharedApplication].idleTimerDisabled = NO; when the application goes into the background, but that doesn't work. How can I safely keep the user's device from sleeping while my app is running?
Alter the idleTimerDisabled property whenever your app changes its active state - if you're going to be backgrounded, re-enable the timer, and when you regain control, disable the timer again.
Here's my solution, using XCode 6.2.
iPhone - phone goes to sleep even if idleTimerDisabled is YES
Basically, even now, in 2015, the only way to safely make sure that the device doesn't go to sleep is to repeatedly call a piece of code to keep the device awake.
-(void)callEveryTwentySeconds
{
// DON'T let the device go to sleep during our sync
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}

cancelAllLocalNotifications in applicationWillTerminate?

I want to cancelAllLocalNotifications and setApplicationIconBadgeNumber to 0 when my application is terminated (either by the OS or by double tapping the home button and killing the app). I simply added these two calls to my primary app delegate code:
-(void)applicationWillTerminate:(UIApplication *)application
{
[application cancelAllLocalNotifications];
[application setApplicationIconBadgeNumber:0];
}
Existing notifications are still running and the badge number remains set?
applicationWillTerminate: is not called by the OS when it kills your app. The OS just kills the process without notifying your app about it. There is no documented way to execute code at this moment.
You should use applicationWillResignActive: instead.
Starting with iOS 4.0 applicationWillTerminate is not called for applications when they are killed in the background. Apps are put in the background when you hit the home button now or otherwise switch apps.