iOS ~ Backgrounding, Suspend, Inactive
-
When running my app on iOS 9, that is supposed to run in the background, once the app goes to Qt.ApplicationInactive, iOS will stop all processing.
My app collects the user's location. I have updated the info.plist to account for required background modes and the location services. But, processing still stops.
On iOS 6.1, however, this does not happen, processing continues when the app is backgrounded.
Is this caused by the transition from Qt.ApplicationSuspended to Qt.ApplicationInactive?
How do I get around this?
--Sam -
According to StackOverflow, we should be using this to enable background locations in iOS 9...but how do we do that with Qt?
if ([self.locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}http://stackoverflow.com/questions/32758125/ios9-not-tracking-location-while-in-background
-
Ok, Trying to incorporate the Objective-C calls necessary for iOS 9. Here is what I have:
.h:
#ifndef _iosLocation_h
#define _iosLocation_hclass IOSLocation
{
public:
static void EnableIOSBackgroundLocation();
};#endif
.mm:
#include "ioslocation.h"void IOSLocation::EnableIOSBackgroundLocation()
{if ([self.locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
if ([self.locationManager respondsToSelector:@selector(pausesLocationUpdatesAutomatically:)]) {
[self.locationManager.pausesLocationUpdatesAutomatically:NO];
}
}And in my main.cpp file, I call "IOSLocation::EnableIOSBackgroundLocation()".
When compiling, I get an error about 'self'...so what should I be using? Pardon the ignorance, but this is my first attempt at mixing Objective-C and CPP and QT and QML and ... and...
-
To the best of my knowledge Qt pauses the event loop when the app gets into background (which is good with respect to energy saving e.g.). For location tracking you will probably have to use and implement the native NSLocation stuff (at least this is the way I am doing it).
-
I am doing this, which works in the iOS simulator (iOS 9, iPhone6), but not on the phone.
I call EnableIOSBackgroundLocation() in my main.cpp file. The return Boolean sets a QML Image to be visible (either a green GPS icon on success or a red one on failure).
In the simulator, the green GPS icon shows up...and the location continues to be logged when the app is put in the background. However, on a real iPhone 6 running iOS 9, the GPS is killed after a couple of minutes when the app is backgrounded.
Anyone have any ideas why?
iosLocation .mm:
#include "ioslocation.h"
#import <CoreLocation/CoreLocation.h>
bool IOSLocation::EnableIOSBackgroundLocation()
{
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];if ([locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates:)]) {
[locationManager setAllowsBackgroundLocationUpdates:YES];
if ([locationManager respondsToSelector:@selector(pausesLocationUpdatesAutomatically:)]) {
[locationManager setPausesLocationUpdatesAutomatically:NO];
return true;
}
else
{
return false;
}
}
return false;
}iosLocation .h:
#ifndef _iosLocation_h
#define _iosLocation_hclass IOSLocation
{
public:
static void EnableIOSBackgroundLocation();
};#endif
-
I've had a thought on what may be happening. Since the locationManager instance is declared inside the EnableIOSBackgroundLocation function, is it possible that it is not persisting beyond that function? And therefore those CLLocationManger values are not remaining after the function exits?
If that is the case, then how can I create a locationManger variable that persists throughout the application? How can I declare it and access it from the QT code?
-
Ok, more info...
Using this code:
*.h#ifndef _iosLocation_h
#define _iosLocation_hclass IOSLocation
{
public:
static bool EnableIOSBackgroundLocation();
};#endif
*.mm:
#include "ioslocation.h"#import <CoreLocation/CoreLocation.h>
bool IOSLocation::EnableIOSBackgroundLocation()
{
CLLocationManager *myLocMan;
myLocMan = [[CLLocationManager alloc] init];if ([myLocMan respondsToSelector:@selector(allowsBackgroundLocat ionUpdates]) {
myLocMan.allowsBackgroundLocationUpdates = YES;if ([myLocMan respondsToSelector:@selector(pausesLocationUpdates Automatically]) {
myLocMan.pausesLocationUpdatesAutomatically = NO;// Request location authorization
[myLocMan requestAlwaysAuthorization];// Set an accuracy level. The higher, the better for energy.
myLocMan.desiredAccuracy = kCLLocationAccuracyBest;// Specify the type of activity your app is currently performing
myLocMan.activityType = CLActivityTypeOtherNavigation;// Set distance filter to NONE
myLocMan.distanceFilter = kCLDistanceFilterNone;return true;
} else {
return false;
}
}
return false;
}And debugging with XCode while running on an attached iPhone... SOME of the variables are set correctly (such as allowsBackgroundLocationUpdates and myLocMan.activityType). However, the other essential pieces do not get set (pausesLocationUpdatesAutomatically, myLocMan.distanceFilter and myLocMan.desiredAccuracy).
So, I again did more Googling...and have come up with a possible reason. It seems to be that the 'extra' parameters can only be set IF the delegate of CLLocationManager is set... does that sound right to anyone?
Being as that may be the case...how can I set it? I've pulled code from https://github.com/colede/qt-app-delegate, but am not sure exactly how to get the AppDelegate and set it properly.
Anyone???
-
And...with more searching and headscratching... I see that MANY examples of using CoreLocation set the delegate to 'self'...
So that raises the question... How & Where do I get 'self' when compiling this QT project in XCode? I've tried, but I keep getting 'self is undefined'.
-
Hi,
self
is the Objective-C version of C++this
-
So, as indicated by SGaist here, Anda_Skoa over at qtCentre and 'Eskimo' over on the Apple Dev Forum, this is an Objective-C issue.
However.. since I am trying to use Objective-C in my QT app, the issue then becomes...How Do I Use Objective-C?
In short, the posted code runs without any real issues. Just some of the variables are not getting set as I need them to. Not being an Objective-C (or iOS) guru, I have to ask the noob question... Is the code I posted truly Objective-C? If it is not, then how do I make it Objective-C (so I can get to the 'self' object), and call it from my QT C++ functions?
-
There's one important thing: there's already a CLLocationManager used by the QGeoPositionInfoSource iOS plugin.