Latest 0.2.0
Homepage https://github.com/MatejBalantic/MBLocationManager
License MIT
Platforms ios , requires ARC
Frameworks CoreLocation
Authors

Build Status

Location manager provides convenient and easy-to-use access to latest iOS device location. It wraps CoreLocation
with convenient singleton class, relieving you of keeping reference to location manager object.

Main features:

  • subscribe to location updates with 3 lines of code
  • three modes of operation: track user’s location only when application is in use (iOS 8 only), track the location even when in background or track only significant location changes
  • use CoreLocation’s distance filters and accuracy modes
  • easy pause and resume on app going to background or foreground
  • instantiate location manager once, use it wherever you need by subscribing to notification

Integrates with iOS 7 and the new iOS 8 CoreLocation APIs.

Now also fully unit tested.

Installation

Installation should be done via Cocoa Pods.

Install CocoaPods if you didn’t yet:

sudo gem install cocoapods
pod setup

Change to the directory of your Xcode project, and Create and Edit your Podfile and add following line

pod 'MBLocationManager'
pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file)

Usage

1. Add required .plist entries

As of iOS 8 you are required to define a message that will be presented to the user on location authorization request. You define this message into your app’s *-Info.plist file. You need to add at least one of the following keys, depending on which location update mode you request:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

Make sure you enter this key in the right .plist file (common mistake is entering it into test-Info.plist). Also do not forget to add the actual message text as a value. Failing to add the key(s) and it’s values to the plist file will prevent your app from asking for user permission and thus will render location updates useless.

2. Start tracking location

To start tracking location on the device, call startLocationUpdates:distanceFilter:accuracy from
application:didFinishLaunchingWithOptions in the AppDelegate.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[MBLocationManager sharedManager] startLocationUpdates:kMBLocationManagerModeStandardWhenInUse
                                             distanceFilter:kCLDistanceFilterNone
                                                   accuracy:kCLLocationAccuracyThreeKilometers];

    return YES;
}

3. Subscribe to the notification events

From the viewcontroller where you intend to use a location of the device, subscribe to notification kMBLocationManagerNotificationLocationUpdatedName
which is triggered whenever a new location is detected. When notification is posted, access
MBLocationManager’s property currentLocation

Notification kMBLocationManagerNotificationFailedName informs you about failed attempt to determine
the location (see locationManager:didFailWithError )

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeLocation:)
                                                 name:kMBLocationManagerNotificationLocationUpdatedName
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(locationManagerFailed:)
                                                 name:kMBLocationManagerNotificationFailedName
                                               object:nil];
}

-(void)locationManagerFailed:(NSNotification*)notification
{
   NSLog(@"Location manager failed");
}

-(void)changeLocation:(NSNotification*)notification
{
    CLLocation *location = [[MBLocationManager sharedManager] currentLocation];
    NSLog(@"Location changed to %@", self.locationLabel.text);
}

4. (Optional) Pause location updates when in background

To improve battery life, you might prevent app from tracking location changes when
entering background.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   [[MBLocationManager sharedManager] stopLocationUpdates];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[MBLocationManager sharedManager] startLocationUpdates];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    [[MBLocationManager sharedManager] stopLocationUpdates];
}

Fine-tune CoreLocation settings

By accessing locationManager property you are able to fine-tune CoreLocation settings, such as
activityType and pausesLocationUpdatesAutomatically.

CoreLocation documentation:
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/cl/CLLocationManager

Latest podspec

{
    "name": "MBLocationManager",
    "version": "0.2.0",
    "summary": "Location manager provides convenient and easy-to-use access to latest iOS device location.",
    "description": "tttLocation manager provides convenient and easy-to-use access to latest iOS device location. It wraps CoreLocationntttwith convenient singelon class, releaving you of keeping reference to location manager object. ntttntttMain features:nttt* subscribe to location updates with 3 lines of codenttt* two modes of operation: GPS (accurate with low battery performance) and cell towers (inaccurate with better battery performance)nttt* use CoreLocation's distance filters and accuracy modesnttt* easy pause and resume on app going to background or foregroundnttt* instantiate location manager once, use it wherever you need by subscribing to notificationn",
    "homepage": "https://github.com/MatejBalantic/MBLocationManager",
    "license": {
        "type": "MIT",
        "file": "LICENSE.txt"
    },
    "authors": {
        "Matej Balanticu030c": "[email protected]"
    },
    "social_media_url": "http://twitter.com/skavt",
    "platforms": {
        "ios": null
    },
    "source": {
        "git": "https://github.com/MatejBalantic/MBLocationManager.git",
        "tag": "0.2.0"
    },
    "source_files": [
        "Classes",
        "MBLocationManager/MB*.{h,m}"
    ],
    "frameworks": "CoreLocation",
    "requires_arc": true
}

Pin It on Pinterest

Share This