How to keep the screen on an iPhone with Phonegap 2.7

风格不统一 提交于 2019-12-08 06:01:55

问题


I do an ios application (ios6) with cordova 2.7 which use the GPS and I need that the screen of the Iphone is always on. I want to avoid the phone from sleep.

I tried to install this plugin https://github.com/phonegap/phonegap-plugins/tree/master/iOS/PowerManagement but apparently it is too old.

How do that?


回答1:


If you're willing to get your hands dirty, this can be quite easy to fix, by making a Cordova Plugin.

If you haven't made a Cordova plugin, it is definitely a skill that you should master, as not all functionality is available through PhoneGap plugins yet, and it can frequently be quite easy to make it work. The full details are available in the PhoneGap Plugin Development Guide.

For this, your JavaScript side of the plugin would be something like

cordova.exec(function(winParam) {}, function(error) {}, "myPlugin", "preventSleep", []);

In this case, your PhoneGap would look for a method named preventSleep in the myPlugin class.

Next, your preventSleep method would look something like

- (void)preventSleep:(CDVInvokedUrlCommand*)command
{
    [UIApplication sharedApplication].idleTimerDisabled = YES;

    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

Now you could make a corresponding method like allowSleep that set [UIApplication sharedApplication].idleTimerDisabled = NO;




回答2:


Here's an up-to-date plugin also supported by PhoneGap Build: https://github.com/EddyVerbruggen/Insomnia-PhoneGap-Plugin




回答3:


I've updated this plugin for more recent versions of Phonegap (2.5 to 2.9) - you can find the source code and my XCode test project in my answer to this question.

Are you sure you want to keep the screen on all the time? Apple are fairly strict on when this is allowed and may reject your app if it's not for a good reason. If you just need to allow you app to keep running and receive position updates in the background, this can be done without a plugin by adding the following to your project .plist:

<key>UIBackgroundModes</key>
<array>
  <string>location</string>
</array>

Or in XCode, open the .plist and add the key "Required Background Modes" and value "App registers for location updates"




回答4:


I'm not sure if you've already seen this but it looks like there's a plugin available for iOS on phonegap build, apparently an android version is coming out soon.

Link here



来源:https://stackoverflow.com/questions/17451553/how-to-keep-the-screen-on-an-iphone-with-phonegap-2-7

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