mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2024-12-22 22:25:38 +07:00
Add files via upload
This commit is contained in:
parent
df64c76a99
commit
c0ec74cfd4
63
Helper/LocationManager.swift
Normal file
63
Helper/LocationManager.swift
Normal file
@ -0,0 +1,63 @@
|
||||
//
|
||||
// LocationManager.swift
|
||||
// DispatchDemoApp
|
||||
//
|
||||
// Created by JungpyoHong on 5/1/21.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import CoreLocation
|
||||
|
||||
class LocationManager: NSObject {
|
||||
|
||||
var locationManager = CLLocationManager() {
|
||||
didSet {
|
||||
locationManager.delegate = self
|
||||
}
|
||||
}
|
||||
|
||||
private weak var controller: UIViewController?
|
||||
var locationList: [CLLocation] = []
|
||||
|
||||
init(presentingController controller: UIViewController) {
|
||||
self.controller = controller
|
||||
super.init()
|
||||
}
|
||||
|
||||
func requestAuthorization() {
|
||||
locationManager.requestAlwaysAuthorization()
|
||||
locationManager.requestWhenInUseAuthorization()
|
||||
}
|
||||
|
||||
func checkAuthorizationStatus() {
|
||||
let status = CLLocationManager.authorizationStatus()
|
||||
|
||||
switch status {
|
||||
case .denied, .restricted, .notDetermined:
|
||||
// show alert
|
||||
break
|
||||
case .authorizedAlways, .authorizedWhenInUse:
|
||||
locationManager.startUpdatingLocation()
|
||||
|
||||
@unknown default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LocationManager: CLLocationManagerDelegate {
|
||||
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
||||
print(error)
|
||||
}
|
||||
|
||||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||
locationManager.stopUpdatingLocation()
|
||||
if let location = locations.last {
|
||||
locationManager.stopUpdatingLocation()
|
||||
let lat = location.coordinate.latitude
|
||||
let lon = location.coordinate.longitude
|
||||
locationList.append(CLLocation(latitude: lat, longitude: lon))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user