App won't relaunch when monitoring CLLocationM

2020-07-30 01:18发布

问题:

I am working on an app that keeps track of user's location at a time-interval set by the user himself(e.g. every 5 minutes) and sends it to a server page by ASIHTTPRequest.

This app should be able to receive updates on foreground, either on background or even when the app is not running(location services).
Although my app successfully receives updates on the foreground and background, it does not seem to wake up when it is not running and do not send me up any requests to the server.

I am using CLLocationManager and its delegate to perform startMonitoringSignificantLocationChanges for when it is on the background.

On Settings, the icon for my app in Location Services appears with a purple arrow as expected.

On my info.plist, I have Required Background Modes set with an item locations as required and methods:

locationManager:didUpdateToLocation:fromLocation:
locationManager:didFailWithError:

implemented.

My method:

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

is also implemented and checks whether launchOptions contains UIApplicationLaunchOptions- LocationKey before starting the significant locations monitoring for when it should wake up / relaunch.

Is there any way I can find out whether my app is really being relaunched?
Is there any extra config that needs to be set in order this to work?

Please let me know if I should provide any additional info.


Specs I am using:
SDK Xcode 4.2.1
CLLocation is inside a Singleton class (read somewhere this might impact)
iOS deployment target: 4.3
Tested on Iphone 3GS,4 and Xcode's iOS 5 simulator, same behavior happens to all of these devices.
Devices:universal


UPDATE

I inserted my code inside -[UIApplication beginBackgroundTaskWithExpirationHandler:] and tried to check against errors with UIBackgroundTaskInvalid. However, it does not even seem to enter the (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.

I am keeping track of the app by trying to save and retrieve data by using SQLite. When executing on both foreground and background, it records the data with no problem. When the app is not running, no data is saved at all.

回答1:

How are you currently checking to see if the app relaunches? Are you logging anything?

The app will relaunch to the background, so you won't see your app come alive. It will actually shut down automatically after some time again. You can do some work and request more background time using -[UIApplication beginBackgroundTaskWithExpirationHandler:].

Another answer here on SO posted some example code. From your description I cannot see anything missing, compare your code to that project to see if your missing something.