New to iOS - Need help to prevent app from dimming screen/going to sleep

Hi, I'd greatly appreciate any help with this problem. I have developed an app for both iOS and android. The app is built with Kivy and Python, it functions as expected however in Xcode I cannot find a way to prevent the phone/device from dimming and locking.

I'm very new to Xcode and iOS, but I ave tried several variations of.

[UIApplication sharedApplication].idleTimerDisabled = YES;

in multiple areas of the m Main but I cannot get it to work, no matter what I try the phone still goes to sleep/locks.

If that code above is supposed to work, I have no idea where to put it. As far as I can tell, the only places I could enter something like that is in the main.m, bridge.m and bridge.h code; or somewhere in the info.plist

I'm currently lost, feels like a simple problem.

Answered by Wormi in 720442022

took me forever but I hope this helps someone else.

For Kivy-iOS, this needs to be entered into the main.m file in Xcode, at the bottom of the main() function.

int main(int argc, char *argv[]) {
    int ret = 0;  

that's the start of main()

then all the main code, with the following code near the bottom.


    // If other modules are using the thread, we need to initialize them before.
    PyEval_InitThreads();

    // Add an importer for builtin modules
    load_custom_builtin_importer();

then, You need to put the below code after that

    // Prevent sleep
        [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block: ^(NSTimer * _Nonnull timer) {
            [UIApplication sharedApplication].idleTimerDisabled = NO;
            [UIApplication sharedApplication].idleTimerDisabled = YES;
            NSLog(@"idle timer");
        }];

        NSLog(@"Initializing python");
    Py_Initialize();

hope this helps someone else.

I suspect that putting the code in main.m is too early. It wants to access the "shared UIApplication" but in main() that hasn't been created yet. Maybe.

I think you probably need to ask the Kivy people about how to do this. There must be some way of calling arbitrary objC or Swift code from slightly later on.

In Swift, that should be called in appDidFinishLaunching.

UIApplication.shared.isIdleTimerDisabled = true

I don't know what's the equivalent in Kivy.

Accepted Answer

took me forever but I hope this helps someone else.

For Kivy-iOS, this needs to be entered into the main.m file in Xcode, at the bottom of the main() function.

int main(int argc, char *argv[]) {
    int ret = 0;  

that's the start of main()

then all the main code, with the following code near the bottom.


    // If other modules are using the thread, we need to initialize them before.
    PyEval_InitThreads();

    // Add an importer for builtin modules
    load_custom_builtin_importer();

then, You need to put the below code after that

    // Prevent sleep
        [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block: ^(NSTimer * _Nonnull timer) {
            [UIApplication sharedApplication].idleTimerDisabled = NO;
            [UIApplication sharedApplication].idleTimerDisabled = YES;
            NSLog(@"idle timer");
        }];

        NSLog(@"Initializing python");
    Py_Initialize();

hope this helps someone else.

New to iOS - Need help to prevent app from dimming screen/going to sleep
 
 
Q