I have been talking about implementing the functionality of location based Android apps in my previous blogs. Here let’s talk about it does this functionality in iOS. We know that using location-based information in your iOS app is quite a good way to keep your users hooked up and it is easy to keep them updated with the new updates. Location based information can be used for practical purposes (such as navigation) or for mobile applications which are of entertainment genre. Location based information can enhance the overall user experience.
When we talk about the location-based information in context of iOS hyperlocal application development it precisely comprises of two main parts: Location Services and Maps.
- Location services are provided by the Core Location framework, which uses interfaces of Objective-C for obtaining information about the user’s location and heading (heading here refers to the direction in which a device is pointing).
- Map Kit framework offers the Maps. Map Kit framework supports both the display and annotation of maps. In order to use the features of Map Kit framework, developers need to turn on the Maps capability in their Xcode project.
Don’t forget to check : Geo-Fencing In Hyperlocal Android Apps – Whats, Whys and Hows and Strategies To Develop Location Aware Hyperlocal Android Apps
Let’s have a look how to know the user’s current location while we are on the move of developing hyperlocal iOS apps:
A – Getting the User’s Current Location
The Core Location framework allows you to get the current position of device and therefore you can use that location update in your application. Developers can make use of two services which can give you the user’s current location:
- Standard Location: Standard Location Service helps in getting the location data and tracking location changes with complete accuracy.
- Significant Change Location: This service triggers the location change prompt and delivers updates only when there is any significant change in location of device, say the device moves 500 meters or more.
A1 – Standard Location Service :
Standard Location Service is a commonly used practice to get the user’s location which holds its availability in iOS and OS X both. Standard Location service fetches the user’s location with complete accuracy of location information and distance travelled. As and when you start this service with specified parameters it determines whether hardware has been enabled or not. Then it proceeds to report all designated location events to your app.
As Standard Location Service takes into consideration accurate parameters, this information is very much suitable for apps which require a fine-grained control on the delivery of location events. Hence, developers can call this service while developing hyperlocal iOS apps. The called service returns very precise value, hence any navigation apps requires complete accuracy of the standard location service. For such service there is always a requirement of location-tracking hardware which needs to be enabled for a longer period of time, hence this consumes lot of power source of the standard location service.
Steps Involved – Calling Standard Location Service:
1: The first step is of creating an instance of the CLLocationManager class. Then to configure desiredAccuracy and distanceFilter properties of this class.
2: Assign a delegate to object and call startUpdatingLocation method for receiving location notifications.
3: As soon as location information becomes available, the location manager informs its assigned delegate object. There can be a chance that location update has been delivered. In such cases a user can fetch the recent location data directly from the CLLocationManager object without having to wait for new event to be delivered.
4: In order to stop the delivery of location updates, user must call stopUpdatingLocation method which lies within the location manager object.
Listing 1 Starting the standard location service
A2 – Significant-Change Location Service
This service can be used if you are looking for an alternative way of power saving. Knowing significant-change location provides accuracy, which is again a good feature for most apps. The service uses Wi-Fi in order to return the user’s location. Also it reports to changes in that location hence allowing a system to manage power usage. Such location service wakes up an iOS app which is currently in suspended mode and is not available for delivering new location data.
Steps Involved – Calling Significant-Change Location Service:
1: Significant-change location service can be used by creating an instance of the CLLocationManager class, and assigning a delegate to it, and then call thestartMonitoringSignificantLocationChanges method.
2: Standard location service, delivers location information in order to delegate object which is well defined in Receiving Location Data.
3: When the location data becomes available, the location manager informs the assigned delegated object. If location update gets delivered, the recent location information can be fetched from CLLocationManager object. You simply do not have to waste time for a new event to be delivered.
4: For stopping significant change location service, you can call the topMonitoringSignificantLocationChanges method.
Listing 2 Starting the Significant Change location service
B- Availability of Location Services:
Often you may deal with a situation where location services are unavailable or not known. For example:
- User can even sometimes disables the location services using the Settings / System Preference option of their device.
- Sometimes user can decline to inform location for any specific application.
- You may face a situation where device is in Airplane mode and hence the desired hardware is unable to to take the power conclusively, no known user location.
For any of the above situation occurrence, you can call the locationServicesEnabled class method which resides in CLLocationManager. This must be done before starting either standard location services or significant-change location services.
It the above call returns “NO” and still you attempt to start location services, the system will prompt the user to ensure that location services must be re-enabled. This happens because users sometimes purposely disable location services and hence the prompt message gets unwelcomed.
C- Tips for Conserving Battery Power
I agree that getting location data in an iOS-based device consumes lot of power. As most of the apps do not require location services running all the time, hence turning off location services when they are not needed is the simplest way to save power.
- Turning off : Turn off location services when you aren’t using them. I know it seems very much a guideline you already heard of. Only navigational apps that offer turn-by-turn directions, most apps do not require location services to be on all the time. Unless the user is not using a moving vehicle, the current location will not get changed frequently enough to be an issue. You can always start location services again if needed (later).
- Use Significant-Change location service : Try using significant-change location service instead of the standard location service if possible. While you can leave the location services running at background, the former services saves a lot of time. Significant-change location services are recommended for apps that do not require higher precision as offered by standard location services.
- Lower-resolution Values : For desired accuracy don’t overdo the things. While you are requesting a higher accuracy it can cause Core Location to power up additional hardware and waste power for precision you don’t need. Unless your app actually requires to know the user’s position within a few meters, it is suggested to not put the values kCLLocationAccuracyBest or kCLLocationAccuracyNearestTenMeters in the desiredAccuracy property.
So, we have discussed how to achieve standard user location or with significant change. Whichever location service you want to use, totally depends on the type of location based iOS apps you are developing. We also saw various tips which can help us in conserving battery power. Here are some cool location aware applications which definitely uses these basic yet very important functionality of iOS application development process. These are PocketEarth, Where, Earthcomber, Remote Showtimes, Food Network.
Happy Coding 🙂 Stay Located 🙂