How to keep iphone screen on in Phonegap 1.0?
I'm trying to keep the iphone screen on while my app's running. 开发者_开发知识库I followed this post: phonegap, iphone and the big bad idleTimerDisabled
and did something like this in PhoneGapDelegate.m:
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// existing code ...
application.idleTimerDisabled = YES; // I added this line
return YES; // existing code
}
but it didn't work.
I also tried this approach: http://groups.google.com/group/phonegap/browse_thread/thread/5eeace5c416719ec/d7180ef5a3a9081d?lnk=gst&q=lock#d7180ef5a3a9081d but it didn't work either.
Could anybody give me some suggestions? Thank you!
UPDATE: Thank you all! It turns out I was changing the wrong fil(PhoneGapDelegate.m). applicationdidfinishlaunching method in that file is never invoked. The file to be changed is AppDlegate.m.
There is a phonegap plugin available on github
It allows to set and reset the "idletimerdisabled" directly from the .js. It's also cleaner since you don't modify your phonegap code and will also work on android.
According to this question: iOS: How to stop the device standby timer? all you need to do is:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[application setIdleTimerDisabled:YES];
return YES;
}
Be sure you add this before the return YES;
line to be sure it executes.
Apple recommends you only use this as-needed:
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.
However
According to this post: Phonegap, iphone - applicationDidFinishLaunching not invoking PhoneGap does not call this function, due to the Xcode project file.
Based on what the answers say, I would try implementing the following function as well, into your app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[application setIdleTimerDisabled:YES];
}
I found the following working for me:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSArray *keyArray = [launchOptions allKeys];
if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil)
{
NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
self.invokeString = [url absoluteString];
}
application.idleTimerDisabled = NO;
application.idleTimerDisabled = YES;
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
精彩评论