Coder Social home page Coder Social logo

tdegrunt / mblocationmanager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matejbalantic/mblocationmanager

0.0 3.0 0.0 359 KB

Location manager provides convenient and easy-to-use access to latest iOS device location

License: MIT License

Ruby 6.67% Objective-C 93.33%

mblocationmanager's Introduction

Build Status

MBLocationManager

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

mblocationmanager's People

Contributors

marcelofabri avatar matejbalantic avatar mpurbo avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.