Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 726
Martin Bischof Avatar asked Feb 08 '23 23:02

Martin Bischof


1 Answers

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
like image 189
Hubi Avatar answered Feb 16 '23 12:02

Hubi