What to create or properly use a singleton - ios

I would like to share the coordinates instance and use it on the
DiscoverDetailVc. I had 2 location manager instances on my app, after
couple research and reading, find out that the best approach is to create a singleton that each VC will use. I came up with the following code but the issue is that on the DiscoverDetailVC, DiscoverRestaurantTableViewController.shared.coordinates is nil however when I print the coordinates from the DiscoverRestaurantVC, I can see the coordinates value. What am I doing wrong in order to get it right.
Thanks
class DiscoverRestaurantTableViewController: UITableViewController, CLLocationManagerDelegate {
static let shared = DiscoverRestaurantTableViewController()
let locationManager = CLLocationManager()
var coordinates: CLLocationCoordinate2D?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Default implementation to get the coordinates
coordinates = location.coordinate
}
class DiscoverDetailViewController: UIViewController {
func getDirections (){
let coordinate = DiscoverRestaurantTableViewController.shared.coordinates
guard let location = coordinate.coordinates else {return print("booon")}
}
}

Change coordinates = location.coordinate to DiscoverRestaurantTableViewController.shared.coordinates = location.coordinate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//coordinates = location.coordinate
DiscoverRestaurantTableViewController.shared.coordinates = location.coordinate
}
But it is good to create new class and manage singleton objects. It doesn’t look good to create singleton within TableViewController.
Hope this helps.

Related

Swift 3 - store the User Location and call it from different View Controllers

I'm pretty new in programming and this is my first app, so sorry if the approach is very shabby.
I created a helper method to get the user location, because I need to call it from different view controllers so I thought this was a cleaner way to do it. But I don't know why is not working now (no errors, it just show the general view of Europe). But when it was inside the view controller it worked perfectly fine.
I got this new approach from the course I'm doing and I've been researching in many sources. I've also checked this question but I didn't find any solution yet.
Here is the method I created in the GMSClient file. It will get the user location, but if the user disables this option, it will show the default position (centred in Berlin):
extension GMSClient: CLLocationManagerDelegate {
//MARK: Initial Location: Berlin
func setDefaultInitialLocation(_ map: GMSMapView) {
let camera = GMSCameraPosition.camera(withLatitude: 52.520736, longitude: 13.409423, zoom: 8)
map.camera = camera
let initialLocation = CLLocationCoordinate2DMake(52.520736, 13.409423)
let marker = GMSMarker(position: initialLocation)
marker.title = "Berlin"
marker.map = map
}
//MARK: Get user location
func getUserLocation(_ map: GMSMapView,_ locationManager: CLLocationManager) {
var userLocation: String?
locationManager.requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
map.isMyLocationEnabled = true
map.settings.myLocationButton = true
} else {
setDefaultInitialLocation(map)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
map.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
//Store User Location
userLocation = "\(location.coordinate.latitude), \(location.coordinate.longitude)"
print("userLocation is: \((userLocation) ?? "No user Location")")
}
}
}
}
This file has also this singelton:
// MARK: Shared Instance
class func sharedInstance() -> GMSClient {
struct Singleton {
static var sharedInstance = GMSClient()
}
return Singleton.sharedInstance
}
And then I call it in my view controller like this:
class MapViewController: UIViewController, CLLocationManagerDelegate {
// MARK: Outlets
#IBOutlet weak var mapView: GMSMapView!
// MARK: Properties
let locationManager = CLLocationManager()
var userLocation: String?
let locationManagerDelegate = GMSClient()
// MARK: Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = locationManagerDelegate
GMSClient.sharedInstance().getUserLocation(mapView, locationManager)
}
Anyone has an idea of what can be wrong?
Thanks!
Following what Paulw11 said, I found the faster solution using Notifications.
Send notification from the LocationManager delegate method inside the first view Controller:
class MapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
} else {
initialLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
let userInfo : NSDictionary = ["location" : location]
NotificationCenter.default.post(name: NSNotification.Name("UserLocationNotification"), object: self, userInfo: userInfo as [NSObject : AnyObject])
}
}
}
Set the second view controller as observer. This way I can store the userLocation and use it later for the search request:
class NeighbourhoodPickerViewController: UIViewController, UITextFieldDelegate {
var userLocation: String?
var currentLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(locationUpdateNotification), name: Notification.Name("UserLocationNotification"), object: nil)
}
func locationUpdateNotification(notification: NSNotification) {
if let userInfo = notification.userInfo?["location"] as? CLLocation {
self.currentLocation = userInfo
self.userLocation = "\(userInfo.coordinate.latitude), \(userInfo.coordinate.longitude)"
}
}
I guess the problem is here,
self.locationManager.delegate = locationManagerDelegate
You have created a new instance of GMSClient, and saved it in the stored property and that instance is set as the delegate property of CLLocationManager.
You need to do this instead,
self.locationManager.delegate = GMSClient.sharedInstance()
You need to do this because you would want singleton instance of GMSClient to be the delegate for CLLocationManager and not a new instance. That way your singleton class would recieve the callbacks from
CLLocationManager class.
To understand more about why your code was not working, I would suggest you read more about Objects, Instances, Instance variables, Singletons, Delegate design pattern.

fatal error: unexpectedly found nil while unwrapping an Optional value when getting Location?

I am developing an app using swift I want to fetch current location of the user and want to show in google map so I wrote my code in my class I have included all the functions and methods (i.e) added and imported frameworks core location and also updated plist but I can't able to fetch current location rotationally some time I get the location but some time it crashed.
Here is the code what I am tried.
import CoreLocation
class HomeViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
let location: CLLocation? = locationManager.location
let coordinate: CLLocationCoordinate2D? = location?.coordinate ----> App crossed this line then it will crashed
print(coordinate!)
print(coordinate!.latitude)
print(coordinate!.longitude)
strForCurLatitude = "\(coordinate!.latitude)"
strForCurLongitude = "\(coordinate!.longitude)"}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
print("User allowed us to access location")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while get location \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.startUpdatingLocation()
let locationNew = locations.last
let coordinateNew: CLLocationCoordinate2D? = locationNew?.coordinate
strForCurLatitude = "\(coordinateNew!.latitude)"
strForCurLongitude = "\(coordinateNew!.longitude)"
strForLat=strForCurLatitude;
strForLong=strForCurLongitude;
print("User's Latitude is: \(Double(strForLat!)!)")
print("User's Longitude is: \(Double(strForLong!)!)")
self.locationManager.startUpdatingLocation()
}
Remove this from ViewDidLoad because you can not get location in viewDidLoad.
let location: CLLocation? = locationManager.location
let coordinate: CLLocationCoordinate2D? = location?.coordinate
print(coordinate!)
print(coordinate!.latitude)
print(coordinate!.longitude)
strForCurLatitude = "\(coordinate!.latitude)"
strForCurLongitude = "\(coordinate!.longitude)"
In CLLocationManager's following method best for getting location
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
make a reference to CLLocationManager as variable, like this:
class HomeViewController {
var locationManager = CLLocationManager()
viewDidLoad() {
super.viewDidLoad()
}
// rest of code
}
in this case Location Manager should works properly.

Loading user location data for GeoFire query in viewWillAppear

On my main CollectionView (the app home page), I would like to update the locations (i.e. the cells themselves) based on the closest location to the user. I am storing my locations in Firebase and will be using GeoFire and core location in order to properly list out the locations in distance order from the user.
Other posts have said that the best practice for observers involves loading it in viewWillAppear; however, I have been unable to obtain the user's location before this runs and do not have a centered point for my GeoFire query. Most examples related to CoreLocation involve maps or preselected coordinates, but I wasn't planning on using a map within this controller and want to dynamically load based off of user location.
I have researched loading didUpdateLocations within AppDelegate, but have not been able to call that method there either. What is the best way to get user location coordinates before running a GeoFire query when loading that returned data in viewWillAppear or do you need to set a region and check if a user has entered within that region?
var locationDict = [CLLocation]() //using this variable to pass values outside the function to the query
override func viewWillAppear(animated: Bool) {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let recentLocation = locations[0]
locationDict.append(recentLocation)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
There is quite a lot missing in your code, but in short (what works for me):
.requestLocation in viewWillAppear
.startUpdatingLocation() in viewDidAppear. (so twice).
var justStartedUsingMap = true
In didUpdateLocations:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let myLocation:CLLocation = manager.location else {return}
let location:CLLocationCoordinate2D = myLocation.coordinate
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.12, longitudeDelta: 0.12))
if justStartedUsingMap == true {
self.currentLocationSearch(myLocation)
justStartedUsingMap = false
} else {
locationManager.stopUpdatingLocation()
}
self.map.setRegion(region, animated: true)
}
func currentLocationSearch(_ location: CLLocation) {
// the geofire observer
}

Utility class to return GPS location in swift?

In my app I need the location of the user several times, so I created an utility class to get the coordinates. This is not working as intended, since I want to call a function to directly return the coordinates, but it doesn't work since it's dine asynchronously. I want, in my viewcontroller, to be able to call it as a singleton, like this:
coords = GPSUtil.sharedInstance.getCoordinates()
With use of the following function:
public func getCoordinates() -> CLLocation {
// return the CLLocation here.
}
public func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let latestLocation: AnyObject = locations[locations.count - 1]
initialLocation = latestLocation as! CLLocation
}
At the moment my locationManager just assigns the CLLocation to a property, but I actually want the above method to return it. Can it be done?
Sounds like you should add a var to your utility class, like so:
class GPSUtil {
var latestLocation = CLLocation(latitude: 0, longitude: 0) //initial init
public func getCoordinates() -> CLLocation {
return self.latestLocation
}
public func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.latestLocation = locations[locations.count - 1]
}
}

CLLocationCoordinates from locationManager didUpdateLocations

I am trying to use CoreLocation (once permission is granted) to get a user's CLLocationCoordinate2D, so that I can pass that information to an Uber deeplink (after a UIButton within my TableView is pressed).
I've figured out how to get the coordinates as CLLocations, and turn them into CLLocationCoordinates2D in the didUpdateLocations method, but can't seem to transfer them over to my buttonPressed function.
Can anyone explain how I can properly transfer the coordinates info to the uberButtonPressed method? I am also confused about how to get the locationManager to stop updating location once a suitable location is determined. Any help is much appreciated. By the way I am using this to instantiate Uber: https://github.com/kirby/uber
import UIKit
import CoreLocation
import MapKit
class ATableViewController: UITableViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var location: CLLocation?
var coordinate: CLLocationCoordinate2D?
// Implemented tableView methods etc here...
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as! CLLocation
println("DID UPDATE LOCATIONS \(newLocation)")
location = newLocation
coordinate = location!.coordinate
println("WE HAVE THE COORDINATES \(coordinate!.latitude) and \(coordinate!.longitude)") // this prints along with DID UPDATE LOCATIONS
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("error:" + error.localizedDescription)
}
func uberButtonPressed(sender: UIButton!) {
let senderButton = sender
println(senderButton.tag)
let authStatus: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined {
locationManager.requestWhenInUseAuthorization()
return
}
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
var pickupLocation = coordinate! // fatal error: unexpectedly found nil while unwrapping an Optional value
println(pickupLocation)
// // Create an Uber instance
// var uber = Uber(pickupLocation: pickupLocation)
//
// Set a few optional properties
// uber.pickupNickname = "OK"
//
// uber.dropoffLocation = CLLocationCoordinate2D(latitude: 47.591351, longitude: -122.332271)
// uber.dropoffNickname = "whatever"
//
// // Let's do it!
// uber.deepLink()
//
}
You should move var pickupLocation = coordinate! into your didUpdateLocations. Once assigned, you can call locationManager.stopUpdatingLocation() also from inside 'didUpdateLocation or your value for coordinate with keep updating. After stopping the locationManager, call a NEW function to run the rest of your code currently in func uberButtonPressed
I had the same problem.
Instead of calling the delegate method like this:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
debugPrint("NOT CALLED-- didUpdateLocations AnyObject")
}
I changed into:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugPrint("CALLED-- didUpdateLocations CLLocation")
}
The problem is that you are unwrapping coordinate which is a CLLocationCoordinate2D? with a !. Since coordinate is from location which is a CLLocation?. It is possible that coordinate is nil and location is nil, especially before location services have had a chance to kick in. Only run the rest of uberButtonPressed: for the case where coordinate and location are not nil by using if let currentCoordinate = coordinate?, and in that case, call locationManager.stopUpdatingLocation().

Resources