Giter Club home page Giter Club logo

corelocationdemo's Introduction

CLLocationManager Singleton in Swift4

This is basically for fetching user current location, In this example Singleton class use for share location service in swift4. This class will auto update location when user change current location using Completion Blocks with error handlling as well.

Installation

Drop in the Classes folder to your Xcode project. You can also use CocoaPods or Carthage.

Using CocoaPods

First be sure to run pod repo update to get the latest version available. Add pod 'CoreLocationDemo' to your Podfile and run pod install. Also add use_frameworks! to the Podfile.

  • target 'MyApp'
  • pod 'CoreLocationDemo', '~> 0.1.0'
  • use_frameworks!

https://cocoapods.org/pods/CoreLocationDemo

Updated for Swift 4

Swift 4 Version

Requirements

  • Version 9.2 or higher
  • Apple LLVM compiler
  • iOS 8.0 or higher
  • ARC

Installation

  • Copy LocationService.swift to your project
  • add the key "NSLocationAlwaysUsageDescription" to a property list(Info.plist) and set its value to some message what you want. etc. "We requires access to your phone's location bla bla bla :P" or
  • add the key "NSLocationWhenInUseUsageDescription" if you want to access the user's location normally while running in the foreground.

How to use - FetchLocation Class

  • Just use the function which am using in the ViewController Class:- self.fetchUserCurrentLocation(). You have to use this function in your viewController or any other class. You will get the current location of that particular location.

How to use - Get real time location by Completion Block

  • Just call the below function in your ViewController Class

import CoreLocation

class ViewController: UIViewController {

var userCoords = CLLocationCoordinate2D(latitude: 0, longitude: 0)

override func viewDidLoad() {
    super.viewDidLoad()
    
    //Fetch User Current Location
    self.fetchUserCurrentLocation()
}

func fetchUserCurrentLocation() {
    
    let locationFetch = FetchLocation.SharedManager
    locationFetch.parentObject = self
    locationFetch.startUpdatingLocation()
    locationFetch.completionBlock = { [unowned self] (userCoordinates, error) in
        
        if error != nil {
            print(error?.localizedDescription ?? "")
        }
        
        if let userLocation = userCoordinates as? CLLocationCoordinate2D {
            print(userLocation.latitude, userLocation.longitude)
            self.userCoords = userLocation
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

Contact Me

Mandeep Singh

corelocationdemo's People

Contributors

mandeepsingh1 avatar

Watchers

Carabineiro 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.