IOS App in Background? - ios

I currently have a discussion with my developers, and we're facing an issue.
We are working on an application which should find offers when user is near some latitudes and longitudes in the GPS Position Tracker. It should send a push notification when user is near a position.
The issue is, it works perfectly when app is "on", but when user "quits" the application the app should go in "Background Mode" and still be available to find users gps position and send back to our webservice in the background mode.
How is that possible?
Is this the solution of do you have a better solution?
1. First time user launch the application we find the users position and make call to webservice to get places nearby 100km and save it to local storage incl. the users unique deviceID (UDID).
User quits the application - here it should run in backgroundMode.
User is near a place in our localstorage database for lat and longitude and now we send a push notification to the users device from the localstorage.
Every 20 minutes we make a call for app to refresh all positions for my currently lng and lat to keep it updated ,

You can preconfigure areas for the app to be notified when the user enters those regions without the need for the app to be in the foreground.
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringForRegion:
So get your places of interest and register those places with the above api, then when your app springs back to life, fire your notification off for whatever you are near.
In iOS, the regions you register with the location manager persist
between launches of your application. If a region crossing occurs
while your iOS app is not running, the system automatically wakes it
up (or relaunches it) in the background so that it can process the
event. When relaunched, all of the regions you configured previously
are made available in the monitoredRegions property of any location
manager objects you create.

The feature you are looking is GeoFencing not push notification. It will create a virtual fence to the specific coordinate and when the user enters into specific distance(say 5km) it will call some delegate methods. So we can implement a local notification with necessary message in that.
I think your solution is something complex and i suggest you to go on with GeoFencing
Find a nice a tutorial here
http://www.creativebloq.com/ipad/get-started-geofencing-ios-9122867

Related

Local Region recognition while the app isn't running in iOs

I'm developing a simple application that when i press one button, creates a Local Region. Till this is all ok, the app creates Region and notify correctly when i enter in this, even if my app is in background. Now, i think that my question is so tricky. Can i create an app that while it's running creates a local region, but even when it's NOT running notify that we are entered in the region created before? I want to know if there is a method that allow me to monitoring significant location changes, even when the app is closed. And if this method recognize (with the app still closed) that i'm entered in a Local Region, starts to uploading my location (i don't know, with a costant interval or something).
You can set a region and your app will get notified via the UIApplication's Launch Options. This will launch you app and you can handle the event then. There will be some limits what you can do when the app is still in the background.
This article from Apple should be helpful: https://developer.apple.com/documentation/corelocation/monitoring_the_user_s_proximity_to_geographic_regions
Edit: You also must enable Location updates for Background Modes in the Capabilities.

How does iOS Google Map detect I am entering a car?

The latest iOS Google Map application(currently of version 4.9.0) has a very nice feature, but so far I can not figure out how this is possible.
The feature is: whenever I am entering my car, try to drive somewhere, Google map will send me a notification to my phone, guess where I am going, giving me an estimate of the traffic, even when the phone is in locked state, and the Google map app hasn't been used for days. A screenshot is provied as following:
This is quite amazing to me as an iOS developer. How can it live in the background and detect I am entering a car with such accuracy?
One of my guess is, Google map is using significant location change API. By using this way, the app can be waken in background mode, but, there is still no guarantee it is waken when I am entering my car. And I have already written a test app using this mechanism, although it can be waken in the background, but the timing is not correct, failed to detect I am entering a car or not.
CLLocationManager Class Reference
If you start this service and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives. In such a case, the options dictionary passed to the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.
Any suggestion and help appreciated.
One comment from #alexkent points out my mistake. This is Apple's Maps.app, not Google Maps. All credits belongs to #alexkent.
That notification is from Apple's Maps.app not Google Maps. A new
feature in iOS9 (which you must be running) is the ability to detect
when you are in your car. I imagine this is done by detecting the
Bluetooth signal from the car radio (I have not checked this). I do
not believe there is developer API available for this feature.
#alexkent is right. It works using bluetooth and there is no public API for that. If you close your bluetooth this stops happening.
Geofencing and in general Geolocation accuracy is not that granular.

Receiving push notifications in iOS without alert message even when app is not active

From Apple's Push Notification Guide:
If the target application isn’t running when the notification arrives, the alert message, sound, or badge value is played or shown. If the application is running, iOS delivers it to the application delegate as an NSDictionary object. The dictionary contains the corresponding Cocoa property-list objects (plus NSNull).
I have implemented this in my app and everything works fine. If the app is in focus, the app gets the message directly. If not active, an alert is shown, the app launches when the user clicks the alert, and finally the app gets hold of the message.
Would it be possible, however, in the case of a message arriving when the app is not active, to get iOS to activate the app and pass the message on without showing any message or requiring any user interaction?
I would like this behavior because the push message from my server only might be of interest to the user, depending on her current position. The app works like this: When it starts, it registers for push and tells my server: I am at this position and would like to be notified when something interesting happens near me. At a later point the server sends a message, but since the user might have moved from the area, I would like the app to check the user's position again, and not bother the user if she now is too far from the original position.
I suppose it would be possible to have a background service that notifies the server about the current position every n minutes, but I fear that this will drain the battery.
Any thoughts on this?
Unfortunately, you can't — in iOS — directly open your app when a notification is received. The user must choose himself to open it via the alert displayed by the system.
However, using the background location is not that battery-unfriendly. It depends on the location accuracy you set for your CLLocationManager object.
All the informations about location accuracy can be found here : Location Awareness Programming Guide
In your case, you may want to use the significant location changes methods or the kCLLocationAccuracyKilometer accuracy for example.
Here is a good tutorial to get started : iOS Multitasking: Background Location
Hope this will help.

Getting user's location while making a phone call

I need to get exact location (as much accurate as possible) in background while making a call to a phone number (The call will be made from app). As far as I understand from documentation, I can listen to significant changes of location in background. I am wondering how accurate is a significant update and when will it trigger.
I need the location where the call is made because it will be an emergency call. And it's not a good idea to listen to location in foreground and then make a call because it will be an emergency situation - call will be made immediately.
Is there a solution to get accurate location of user in background? What do you recommend?
Edit: Location will be sent to server immediately.
As for the accuracy, you can get accurate location in the background, under those conditions:
Getting Location Events in the Background
If your application needs location updates delivered whether the application is in the foreground or background, there are multiple options for doing so. The preferred option is to use the significant location change service to wake your application at appropriate times to handle new events. However, if your application needs to use the standard location service, you can declare your application as needing background location services.
An application should request background location services only if the absence of those services would impair its ability to operate. In addition, any application that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation application would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.
As for getting location during phone call, I didn't use it myself, but navigation apps like 'waze' do inform turns and navigation events during a phone call so I guess what you ask is possible.
If I understand your needs, you have 2 options:
If you think that the user will be static then simply get the user location right before the call is made. Or better when your app is lunched to make the emergency call.
If the user is moving then you can 'ask' to get background location events. Even then you should consider using 'significant-change location service' as you don't need more then the user location. The standard 'location services' are used for navigation 'Turn by turn' services.
*LAST COMMENT *
If your app is an emergency app you shold read the "Tips for Conserving Battery Power" in the previous link. you don't want your user battery to empty while tracking his location.
Can't you like add that flag in your app plist file to request Location Service to be running in the background mode.
Then in your app, when the user presses the "call" button, you can do your normal CLLocationManager didUpdateToLocation, right before you perform the call.
In your didUpdateToLocation method, you can record the user's location either in a TXT file written to the application sandbox Library/Cache folder or you can use NSUserDefaults.
Note: I've written an app that records the user's GPS location as they drive even when the user presses the lock screen, so I am confident your app can write the GPS coordinate to a TXT file even when the app is minimized, as long as you start your location manager.

Push Notifications and Services for iOS

I have an app which is tracking the coordinates of the user. I want the user to receive a push notification at a specific longitude and latitude. That means that my device has to track coordinates and make requests even if it's closed.
Is there any way to accomplish this?
Push Notifications can't do this.
CLLocationManager actually has a method, startMonitoringForRegion:desiredAccuracy: to monitor for when a users enters a region and wake your app if needed. See more here. I'm pretty sure this is the system they use for their Reminders app, where you can get a reminder at a specific location. It uses a lot less energy than having your app monitor the location all the time, since the OS can use information like which cell towers or wifi hotspots you are nearby, to figure out if it's even necessary to use the GPS.
Take a look at the document and this
Your app will keep running in the background and receive location updates.Then send local notification to the user. But if the user killed your app manually.The work will not been done.

Resources