Disable automatic screen lock in iOS 5.1 - iphone

The following line of code prevents the app from automatically locking the screen after some idle time.
[UIApplication sharedApplication].idleTimerDisabled = YES; //write this in applicationDidFinishLaunching
It works well till iOS 5.0.
But iOS 5.1 does not respect this line and locks the screen after some idle time.
How to solve this irritating issue?
Thanks.
Edit:
The same code works fine when its installed in 5.0.1 device. But I dont know why it is not working with 5.1 device.

Just setting [UIApplication sharedApplication].idleTimerDisabled = YES; in
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
works well for me. However, there is a caveat. I have noticed that every time I invoke camera utility to take a snapshot, idleTimerDisable gets set to NO behind the scene. So right after I upload my image, I had to call the following line of code again:
[UIApplication sharedApplication].idleTimerDisabled = YES;
I would not be surprised if there are more places throughout that require same strategy. So far this approach has worked without issues for me.

[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
worked for me on iOS 5.1

No there should be no difference. Perhaps you have another mistake..
See iOS 5.0 to 5.1 API Diffs

Important: You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most
applications should let the system turn off the screen when the idle
timer elapses. This includes audio applications. With appropriate use
of Audio Session Services, playback and recording proceed
uninterrupted when the screen turns off. The only applications that
should disable the idle timer are mapping applications, games, or
similar programs with sporadic user interaction.
Maybe You exceeds the allowable time limit of being awake?

i know its is old, but i found this good and in Swift you can do it look a like this
application.idleTimerDisabled = true
Thanks for you answers! i use right now xcode 7 Beta 3 ( Swift 2 )

For Swift, I use this to do outside of delegate:
UIApplication.sharedApplication().idleTimerDisabled = true

Works fine if your application is registered for some background task, for example GPS location updating.

Related

CBCentralManager changes for iOS 7

I am trying to use Apple's 'BTLE Transfer' sample project to understand CoreBluetooth programming. The app runs fine if I use an iOS 6 device as the Central, but if I run the same app with the iOS 7 device as the Central it doesn't work. The peripheral stops sending after two packets, and the central doesn't receive either one of them.
The only clue is this warning that I get only when running on iOS 7:
CoreBluetooth[WARNING] <CBCentralManager: 0x15654540> is disabling duplicate filtering, but is using the default queue (main thread) for delegate events
Can anyone tell me what needs to change to make this app compatible with iOS 7?
EDIT: When both devices are iOS7 there are no issues. This only breaks when an iOS7 central is talking to a iOS6 peripheral.
Okay I just ran it on an iOS 7 central to iOS 6 peripheral. If you want to make that warning about disabling duplicate filtering go away, just run it on a different thread. Do something like this:
dispatch_queue_t centralQueue = dispatch_queue_create("com.yo.mycentral", DISPATCH_QUEUE_SERIAL);// or however you want to create your dispatch_queue_t
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue];
Now it will allow you to scan with duplicates enabled. However, you must call the textView setter on the main thread to be able to set the text without crashing:
dispatch_async(dispatch_get_main_queue(), ^{
[self.textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];
});
Btw you probably also want to adopt the new iOS 7 delegate initialization:
_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:centralQueue options:nil];//set the restoration options if you want
(Just check the iOS version and call the appropriate initialization method)
InscanForPeripheralsWithServices:options:,if you set CBCentralManagerScanOptionAllowDuplicatesKey:#YES then change it to
CBCentralManagerScanOptionAllowDuplicatesKey:#NOthat means scan should run without duplicate filtering.
For me,it works on iOS7 also.

Allow application continue running when iPhone gets locked

I have developed an iPhone app which gets the microphone recorder inputs every second using NSTimer, calculate the amplitude of sound, and take appropriate action if required. I have noticed that when my iPhone gets locked, my application stops running(it doesn't fire the timer callback function). When I unlock iPhone, it start automatically. Could anyone please tell me how can I allow application responding when iPhone gets locked? Is there any other way around such that iPhone shouldn't get locked when my application is running? What should be Apple's recomendation on this?
I found this article but not sure it is correct way to do.
Thanks.
You cannot prevent timer from being stopped when application goes into idle state. Only one possible way is to disable idleTimer of the application.
application.idleTimerDisabled = TRUE;
//or
//Disable screen dimming if no user input occur on device.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

setIdleTimerDisabled and MPMusicPlayer Issue

Ok I want to disable the idletimer in my app. I put the code below in appdelegate in the applicationDidFinishLaunching method, and it works fine (the device remains unlocked)
[application setIdleTimerDisabled:YES];
Though, when I play a music through MPMusicPlayer methods, the idletimer seems to be enabled back again... and soon the device goes into dimmed mode and soon after, "locked" mode.
I tried to find in google for other methods, but ultimately there are NONE that are effective. I also tried putting the above code in another function and calling that function every seconds using NSTimer when the music is playing, but to no avail. It just won't work.
How can I solve this issue?
Ugh. I hate answering my own questions. But here you go, the answer is here:
How to stop MPMusicPlayerController from enabling screen locking
See the reply from henning. Works like a charm!

CLLocation to work in sleep/screen lock mode (iOs < 4.0)

I need to get the user's location even if the phone goes to sleep/screen lock mode. I've tried enabling location services in background mode by adding key to the info.plist file, but this method requires iOS >= 4.0. Is there any alternate method to get the location manager working in screen lock mode
Yes. Have a look at this:
http://github.com/marcop/iPhoneInsomnia
It keeps the iDevice awake by registering your app as an audioplayer and playing a silent sample every 5 seconds. AFIK, this is the only way to do it in iOS < 4.0.
It seems like a hack, so I don't know what Apple thinks of it, still there are a lot of apps in the AppStore that apparently does this.

idleTimerDisabled not working since iPhone 3.0

I have used:
[UIApplication sharedApplication].idleTimerDisabled = YES;
in a number of Apps developed and running under iPhone OS 2.x and never had any problems with it. They were clock apps so needed to run constantly and ignore the iPhone's idle Timer setting.
However, trying to achieve the same with a new App running OS 3.0 (and which needs to be deployed under 3.0 as it uses some 3.0 APIs) I've found the idle Timer to be either ignored or inconsistent.
My App plays music from the iPod library and when the music is playing it auto-locks regardless of the above setting. But once you unlock it, it then doesn't auto-lock again unless you play music again, in which case it locks again after the iPhone auto-lock time setting.
I'm amazed no-one else has come across this as I imagine it would affect a large number of Apps.
Just to clarify:
1. The above code is in ApplicationDidFinishLaunching
2. I know that the phone won't auto-lock when testing from xCode regardless of settings
If anyone has any thoughts I'd be very grateful...
Our app uses the MPMediaPLayer. We also had the idleTimerDisabled=YES code in the ApplicationFinishedLaunching, which works EXCEPT if untethered, and there is already a current nowPlayingItem which is left playing (or unpaused, if paused at app startup). Obviously this is all with the Settings -> General -> Autolock set to some timed value.
By adding idleTimerDisabled=NO, immedately followed by idleTimerDisabled=YES in one of the other bits of code AFTER we had figured out what bit of music we would get playing seemed to solve the problem. Just setting it to YES was insufficient.. and subsequent queries had always indicated the correct value (YES).. so it appears the Apple code ignores the setting of the value IF there is a current piece of music and that is not changed by your code.. but does notice a change of value.
This is all under iOS 3.0.
Even in 2015, using iOS 8.2, this bug is still alive and kicking.
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];
}
Sounds like a bug, file with Radar - I am not too surprised this has not been seen much as there are probably not a lot of apps that try to lock the screen open and play music.
Having same issue. It does work when the device is plugged in. You can press lock button on top, and my NSTimer fires later and causes a vibrate. However if the device is not plugged in pressing the lock button puts the device to sleep. Any solution would be greatly appreciated.
iCodeblog posted about the idletimer, I said it didn't work, and the person who develops 'cute clock' was nice enough to reply. You have to do a hack, play a 1 second or longer silent sound every 10 or so seconds with NSTimer. This keeps the device awake even if the user hits the lock button.
I develop Seconds - Interval Timer for iPhone and iPod touch and I've had no end of trouble with this. The idea of my app is that people create timers based on a number of intervals where each interval can have it's own playlist or track played.
In iOS3 I had the problem that I couldn't disable the idle timer by just setting idleTimerDisabled = YES. In the end I came up with the same solution as Neil whereby I would periodically set it to NO, then immediately to YES again. This seemed to work.
I'm now updating the app to iOS4 (I know, iOS5 is just around the corner...) and now I have the opposite problem. If the MPMediaPlayer changes track before the idle timer reaches it's limit it gets reset. I've just tested this by creating an interval in my app that was 55 seconds, my auto-lock was set to a minute. At 50 seconds the screen dimmed as it prepared to lock, but at 55 seconds when the music changed it went back to full brightness and then didn't lock as it should.
Overall, the implementation of this seems flakey at best.