4

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?

NG_
  • 6,895
  • 7
  • 45
  • 67
Martin Bischof
  • 115
  • 1
  • 6

1 Answers1

8

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
Hubi
  • 1,629
  • 15
  • 20
  • thanks Hubi! But now I have the problem that qt can't find Foundation if i compile it for Android. My app should work on both operating systems. Can you give me a further hint? – Martin Bischof Sep 25 '15 at 11:05
  • Now I can compile everything and the App runs on the IOS, but unfortunately the display doesn't keep awake. And in the Qt Creator the [[UIApplication ... line is displayed as unknown. Can you help me one more time? :) – Martin Bischof Sep 28 '15 at 06:31
  • OK, now it works! I just had to add the line "LIBS += -framework UIKit into the ios { } part of the .pro file. Thank you very much, Hubi! – Martin Bischof Sep 28 '15 at 07:00
  • This worked great! My app updates firmware on a Bluetooth connected device. Updates take 10 minutes and would fail due to iOS auto-lock. While the iOS Settings > Display > Auto-Lock > Never, would prevent the problems, this solution is much better. Thank you! – Ed of the Mountain Sep 27 '16 at 15:04