Update MKMapView when app is in background - ios

I have code in my iOS app that uses MKMapView to run certain code when the user moves:
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
// The code I want to run
}
I was wondering if it was possible to keep updating even if the app was in the background, or if not, how to replicate this code in CLLocationManager.
EDIT:
I need this code to get the position of the user in the background

I was wondering if it was possible to keep updating even if the app was in the background, or if not, how to replicate this code in CLLocationManager.
There's no point in updating a map view if the view isn't being displayed on the screen.
If you want to do something even when the map view isn't visible, you should use Core Location. All you need to do is to create an instance of CLLocationManager, give it a delegate, and tell it to start updating. Your location manager delegate can implement -locationManager:didUpdateLocations: to get location updates, and you can do whatever you like with the updated information.
Note that you shouldn't unnecessarily use location when the app is in the background. Read more about getting updates in the background in Getting Location Updates in the Background.

Related

Do not show location service icon in status bar but still use background updates

I'm looking for some solution to handle the problem:
If I use locationManager.requestAlwaysAuthorization() there're appears an location icon in status bar and never disappears, so, it is the problem - user can think that my app drain battery, but it's not.
So, when I use .requestWhenInUseAuthorization() I can't call didUpdateLocations function to update location even I add fetch background update, the #selector does not allow to call that method.
I want my app get location updates in background without always showing the location icon in the status bar. How can I update location by fetch?
So, when I use .requestWhenInUseAuthorization() I can't call didUpdateLocations function
Yes you can. If you go into the background when you are updating locations, you can continue to update locations. What you cannot do is start updating locations from the background.
I want my app get location updates in background
Use deferred location updates or location monitoring. This will keep your use of sensors to the minimum. Otherwise, the user's impression that you are using up the battery while in the background is entirely justified.

How to stop running app in background when not needed?

In my app I'm using location updates in the background and this works without a problem. The issue is when the user doesn't need the location updates and goes out of the app, the app is still running in the background. I added some code to the appdelegate method:
- (void)applicationDidEnterBackground:(UIApplication *)application
Here I set location manager to stop updating location and also set it to nil. This doesn't solve the problem.
Is there a way for the app to behave like a normal app when the user doesn't need background updating? In this version is frustrating for the user to see the location arrow in the status bar even if the location is not updating. The location manager is stored in a singleton, so there is only one instance of manager in the whole app.

iOS - How to get rid of "app is using your location" notification

I'm using Google Map SDK 1.8.1 on ios 8,
I set Allow Location Access While Using The App:
And then everytime I using GMSMapView and Home, this one shows up.
(When there is an UIViewController that contains GMSMapView in navigation stack)
How can I dismiss it programmatically ?
Thanks you.
edit: sorry, I didn't make my question clear enough.
What I mean is how can I stop GMSMapView from using Location Service when users press Home button, I think that will do the trick.
I tried Google Map's app, the notification view does disappear when you Home, so I think it is possible.
You can't prevent the banner from displaying while you're using the user's location, it's information for the user provided by the system, for them to make informed decisions about which apps they share their location with and when.
Even if you found a way to do this, I'd be extremely surprised if it would get through app review. There's no specific rule about it, as it's not an option in the API anyway, but the gist of the guidelines is that the user should know what you're doing with their location, and when you're tracking it:
4. Location
4.1 : Apps that do not notify and obtain user consent before collecting, transmitting, or using location data will be rejected
Edit - responding to home button press
In order to stop using location when the user presses the home button, you need to subscribe to notifications about entering the background. When you initialise your class which is using location, register for the following:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
Then, in the method which is called for that notification, stop using location:
-(void)appWillResignActive:(NSNotification*)note
{
//stop using location here. I'm not familar with GMSMapView, but assuming you have a reference to the map view, the documentation suggests this:
self.mapView.myLocationEnabled = NO;
}
You may also want to register for the UIApplicationWillEnterForegroundNotification so that you can pick location services back up again when the user opens your app again from the app switcher.
No way to hide it. Even if I stop Location updates.
One thing is for sure:
You must use NSLocationAlwaysUsageDescription in info.plist
and
request requestAlwaysAuthorization from LocationManager
in order to
hide the blue bar forever (and still have the background capability because of Location updates).
PS: I've seen someone speaking about having the blue line disappear after a while but that did not happen to me. Can anyone confirm of auto hiding blue bar?

iOS : Detect user location when App is back from Background

I have a MKMapView & I use it to display user's current location. It works normal when I first load it in the App. But when I press Home button to put the App to background & call it back from background, the map prompts it is unable to find the user's location.
I was thinking is the MKMapView takes time to search for user's location, and when I call it back from background, it does not have enough time to load the location service ?
iOS5 SDK, xCode 4.3 is used.
If I understand your question correctly, your problem is that when your app resumes (becomes active), mapView.userLocation returns nil. Although I haven't tested this, I suspect it is because the mapView will need some time to relocate the user. You can get around this by adding your logic to the appropriate delegate method instead, which is - (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation, if memory serves me right (you might want to verify that, though).
EDIT: Make sure you test location-related code on a device, since its behaviour might differ from the simulator.

How to force ViewController to unload?

I have an application that runs on background because it's needs to use Location Update. however, since i don't want to consume battery. I would like to make sure that all view controller is unloaded when app switch to run on background.
Forcing your view controller to unload is not going to save any battery life. It's best to let the operating system worry about taking your app out of memory when it needs to. If you're using the background mode that notifies your application when the user has travelled a significant amount you need to worry about this even less since your app is not actually running in the background but only given a chance to respond to location changes. If you're using the constant location background mode (for GPS apps, etc.) this is a little bit more of an issue.
You don't need to unload the view controllers, but you do need to turn off location updates. If you are starting location updates in your App Delegate, put in something like this:
- (void)applicationWillResignActive:(UIApplication *)application
{
[myLocationManager stopUpdatingLocation];
}
You can also do this in the viewWillDisappear: method if your location manager is started in a different place than the App Delegate.

Resources