phonegap, iphone and the big bad idleTimerDisabled

白昼怎懂夜的黑 提交于 2019-12-06 07:05:41

问题


reading a lot about how to prevent iphone goingto sleep while running my app I am very unhappy at the moment because nothing worked..

here I read about the idea to set up a timer for every 30 seconds to set the idleTimerDisabled to NO and then YES, but my objC isn't that good yet. could anybody tell me how (and where)?

thnx!

edit: here is the code I tried:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{   
    [ super applicationDidFinishLaunching:application ];
    //application.idleTimerDisabled = NO;
    //application.idleTimerDisabled = YES;
    //[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    [UIApplication sharedApplication].idleTimerDisabled = NO;
    [UIApplication sharedApplication].idleTimerDisabled = YES;


}

edit2: after that I tried to start a loop with:

-(void)_timerLoop
{
    // add this function up the top.  it's what will happen when the
    // timer goes off:
    NSLog(@"Your timer went off!!!");
}


/**
 * This is main kick off after the app inits, the views and Settings are setup here.
 */
- (void)applicationDidFinishLaunching:(UIApplication *)application
{   
    [ super applicationDidFinishLaunching:application ];
    //application.idleTimerDisabled = NO;
    //application.idleTimerDisabled = YES;
    //[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    //[UIApplication sharedApplication].idleTimerDisabled = NO;
    //[UIApplication sharedApplication].idleTimerDisabled = YES;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(_timerLoop) userInfo:nil repeats:YES];


}

edit3: you really can't change accidentially downvotes? would be a nice chnage request to the stackoverflow system!


回答1:


Regarding your question about using a timer:

Here is how you make a timer go off (just once) in 30 seconds from now:

-(void)_jump
{
// add this function up the top.  it's what will happen when the
// timer goes off:
NSLog(@"Your timer went off!!!");
}
...
// here's how to create the timer, which will go off in 30 seconds
[NSTimer scheduledTimerWithTimeInterval:30.0
   target:self selector:@selector(_jump) userInfo:nil repeats:NO]

If you want two different timers, after say 30 and then 60 seconds, just make two in the same way. Let me know if you need more help with timers!


It couldn't be easier. Just add this line:

application.idleTimerDisabled = YES;

Inside your "application didFinishLaunchingWithOptions" routine.

You will find that routine inside your app delegate .m source code file.

Be sure to add it BEFORE the "return YES;" statement - a common mistake! So, exactly like this:

-(BOOL)application:(UIApplication *)application
            didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // blah blah ...

    application.idleTimerDisabled = YES;
    return YES;
    }



回答2:


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 from my phonegap app to take a snapshot, idleTimerDisable gets set to NO behind the scene. So right after i upload my image, I had call the following line of code again:

[UIApplication sharedApplication].idleTimerDisabled = YES;

Also, I would not be surprised if there are more places throughout the app idle timer gets reenabled.



来源:https://stackoverflow.com/questions/5824072/phonegap-iphone-and-the-big-bad-idletimerdisabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!