قالب وردپرس درنا توس
Home / IOS Development / Trying to create a CoreLocation custom delegate class for placement in multiple views

Trying to create a CoreLocation custom delegate class for placement in multiple views



I practice building a weather app. I have a control panel on the table asking for location coordinates only once (then download weather with a model class) for later display of controls. However, I realized that I might want to request placement again in the app. After googling I understood that I can create a custom CLLocationManagerDelegate class or a singleton, like:

  protocol LocationServicesDelegate: class {
func someMethod (parameter: somethingINeed)
}

class CustomLocationManager: NSObject, CLLocationManagerDelegate {}

.. and move my CLLocationManagerDelegate methods to this custom class CustomLocationManaer.

However, I run into a problem I do not understand. In OnboardingViewController, I ask the site services with one button press. Then I use CLAuthorizationStatus to call other methods to change the user interface. What do I need to do carefully with the protocol delegate from the display controller? Request authorization status, and if so, what is a better way to update the display user interface? Here is my code:

  import UIKit
import CoreLocation

class OnboardingViewController: UIViewController, CLLocationManagerDelegate {

leave locationManager = CLLocationManager ()
var currentLocation = CLLocation ()

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

// Site delegator
func callLocationDelegate () {
locationManager.delegate = self
}

// Configure location accuracy and when to use GPS
func setupLocationServicesDetail () {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startMonitoringSignificantLocationChanges ()
}

// Check the user's authorization status for local services
func locationAuthorizationCheck () {
leave status = CLLocationManager.authorizationStatus ()
change status {
case. not determined:
locationManager.requestWhenInUseAuthorization ()
return
case .denied, .restricted:
showAlertLocationAccessNeeded ()
return
matters .authorizedAlways, .authorizedWhenInUse:
break
}
}

// Update user interface based on Location Services AuthorizationStatus
func locationManager (_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
change status {
case. not determined:
break
matters .authorizedWhenInUse, .authorizedAlways:
if CLLocationManager.locationServicesEnabled () {
switchButtontoAllowedUI (button: self.allowLocationButton)
continueToAppButton.isHidden = false
}
return
case, limited, denied:
return
}
}

// Success - few placements and coordinates. go to global location
func locationManager (_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
guard let currentLocation = locationManager.location else {return}
Location.sharedInstance.latitude = currentLocation.coordinate.latitude
Location.sharedInstance.longitude = currentLocation.coordinate.longitude
}

// Error - get wrong
func locationManager (_ manager: CLLocationManager, didFailWithError error: Error) {
print ("error :: (error)")
}

@IBAction func allowAccessToLocation (_ sender: UIButton) {
callLocationDelegate ()
setupLocationServicesDetail ()
locationAuthorizationCheck ()
}
}

In some examples, I see the locationManager.requestWhenInUseAuthorization is called in the viewDidLoad view control, and then a start-up of the {locationManager.startUpdatingLocations ()} method is called from the custom CLLocationManagerDelegate class to get repeating locations sent to (didUpdate The Locations 🙂 method and this method is sent to the custom class via the delegate. Now you have placement for future viewing controlled use. However, I need to get placement again and I also need Authorization Status so I make UI changes to this view controller and others, and to get it in viewcontrollers, I would have to import CoreLocation to each view controller and repeat code. Can anyone give me a tip on how to do this or understand this?


Source link