Results 1 to 8 of 8

Thread: iOS ~ Backgrounding & Geolocation

  1. #1
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default iOS ~ Backgrounding & Geolocation

    Asked this on forum.qt.io, but with no help...can someone here offer any insight?
    =========================================
    When running my app on iOS 9 that collects the user's location (typically once per second) and the app is backgrounded, the OS kills the geolocation service. I have updated the info.plist to account for required background modes and the location services according to the Apple docs. But, processing still stops.

    On iOS 6.1, however, this does not happen, processing continues when the app is backgrounded.

    According to StackOverflow, we should be using this to enable background locations in iOS 9...but how do we do that with Qt?

    Qt Code:
    1. if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates:)]) {
    2. self.locationManager.allowsBackgroundLocationUpdates = YES;
    3. }
    To copy to clipboard, switch view to plain text mode 

    http://stackoverflow.com/questions/3...-in-background

    SO, I am doing the following, 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. On a real iPhone running iOS 9, the green GPS icon shows up also, but the GPS is killed after a couple of minutes when the app is backgrounded.

    Anyone have any ideas why?

    iosLocation .mm:

    Qt Code:
    1. #include "ioslocation.h"
    2.  
    3. #import <CoreLocation/CoreLocation.h>
    4.  
    5. bool IOSLocation::EnableIOSBackgroundLocation()
    6. {
    7. CLLocationManager *locationManager;
    8. locationManager = [[CLLocationManager alloc] init];
    9.  
    10. if ([locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates:)]) {
    11. locationManager.allowsBackgroundLocationUpdates = YES;
    12.  
    13. if ([locationManager respondsToSelector:@selector(pausesLocationUpdatesAutomatically:)]) {
    14. locationManager.pausesLocationUpdatesAutomatically = NO;
    15.  
    16. // Request location authorization
    17. [locationManager requestAlwaysAuthorization];
    18.  
    19. // Set an accuracy level. The higher, the better for energy.
    20. locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    21.  
    22. // Specify the type of activity your app is currently performing
    23. locationManager.activityType = CLActivityTypeOtherNavigation;
    24.  
    25. // Set distance filter to NONE
    26. locationManager.distanceFilter = kCLDistanceFilterNone;
    27.  
    28. return true;
    29. } else {
    30. return false;
    31. }
    32. }
    33. return false;
    34. }
    To copy to clipboard, switch view to plain text mode 

    iosLocation .h:

    Qt Code:
    1. #ifndef _iosLocation_h
    2. #define _iosLocation_h
    3.  
    4. class IOSLocation
    5. {
    6. public:
    7. static void EnableIOSBackgroundLocation();
    8. };
    9.  
    10. #endif
    To copy to clipboard, switch view to plain text mode 

    Anyone have any ideas on why this is happening? Ideas on how to get around it and make it work?

    --Sam

  2. #2
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: iOS ~ Backgrounding & Geolocation

    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?

  3. #3
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: iOS ~ Backgrounding & Geolocation

    Ok, more info...

    Using this code:
    *.h
    #ifndef __iosLocation_h_
    #define __iosLocation_h_

    class 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???

  4. #4
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: iOS ~ Backgrounding & Geolocation

    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'.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: iOS ~ Backgrounding & Geolocation

    My guess would be that such information can be found at Apple's developer site.
    After all that is not related to Qt but would apply to all C++ based applications.

    Cheers,
    _

  6. #6
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: iOS ~ Backgrounding & Geolocation

    It IS related to QT/QML, as this is a project I am using QT for....and outputting a XCode project to compile.

    Just hoping someone here had seen this and found a solution.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: iOS ~ Backgrounding & Geolocation

    Quote Originally Posted by scgrant327 View Post
    It IS related to QT/QML, as this is a project I am using QT for....and outputting a XCode project to compile.

    Just hoping someone here had seen this and found a solution.
    I didn't want to imply that this wasn't relevant for this forum, just that an Apple developer site might have a higher chance of yielding results.
    After all the questions is about integrating Objective-C with C++, regardless of whether the C++ is also using Qt.

    Cheers,
    _

  8. #8
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: iOS ~ Backgrounding & Geolocation

    So, as indicated by Anda_Skoa here, SGaist on forum.qt.io 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?

Similar Threads

  1. iOS Geolocation problem
    By TheJCR in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 15th November 2015, 18:53
  2. How to use QtWebkit with geolocation?
    By brcontainer in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2015, 20:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.