Search code examples
iosqtscreenqmlscreen-lock

How to prevent Screen lock ios with Qt


I want to develop an app in Qt for iOS that contains a map. During the use, the screen lock of the phone should be disabled. But I can't find any solution how to prevent the screen lock in iOS using Qt.

How can be done that?


Solution

  • You must use the native iOS api. You can compile ObjC++ code directly with the clang compiler in your Qt application.

    So you can mix .cpp and .mm (ObjC++) files. QtCreator and qmake support this via the OBJECTIVE_SOURCES keyword.

    In a yourclass.mm implementation:

        #import <Foundation/Foundation.h>
        #import <UIKit/UIKit.h>
    
        void YourClass::setTimerDisabled() {
            [[UIApplication sharedApplication] setIdleTimerDisabled: YES] 
        }
    

    yourclass.h:

    class YourClass
    {
    public:
       void setTimerDisabled()
    }
    

    Now you can call from anywhere in your Qt-app:

    YourClass yc;
    yc.setTimerDisbabled();
    

    In your project file (.pro), if you only want this file on iOS:

    ios {
    OBJECTIVE_SOURCES += \
        yourclass.mm \
    }
    

    And if you only want specified code on a single platform, use preprocessor commands in your source and header files like this:

    #if defined(Q_OS_IOS)
       // iOs stuff
    #elsif defined(Q_OS_ANDROID)
       //Android stuff ...
    #else
      //Other stuff ...
    #endif