Music_Player3/MusicPlayer/MP/Common/Tool(工具封装)/MP_LocationManager.swift
2024-05-31 17:05:17 +08:00

168 lines
6.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MP_LocationManager.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/23.
//
import Foundation
import CoreLocation
class MP_LocationManager: NSObject {
static let shared = MP_LocationManager()
var getAuthHandle: ((_ success: Bool) -> Void)?
//
private var locationManager: CLLocationManager!
//
private var location:String = "US"
override init() {
super.init()
locationManager = CLLocationManager()
// 3 kCLLocationAccuracyThreeKilometers
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.delegate = self
}
///
/// - Parameter complete:
func setLocationPermission(_ viewController:UIViewController ,complete:(() -> Void)?) {
if MP_LocationManager().hasLocationPermission() == true {
//
if complete != nil {
complete!()
}
}else {
//
switch MP_LocationManager().locationPermission() {
case .notDetermined:
MP_LocationManager().requestLocationAuthorizaiton()
case .restricted, .denied:
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Location permission request", message: "“Musiclax” needs to obtain your location information in order to refine the preview music information provided to you!", preferredStyle: .alert)
let CancelAction = UIAlertAction(title:"Cancel", style: .cancel)
let OKAction = UIAlertAction(title: "Settings", style: .default) { (action) in
let url = URL(string: UIApplication.openSettingsURLString)
if let url = url,UIApplication.shared.canOpenURL(url){
if #available(iOS 10, *) {
UIApplication.shared.open(url, options:[:], completionHandler: nil)
}else{
UIApplication.shared.canOpenURL(url)
}
}
}
alertController.addAction(CancelAction)
alertController.addAction(OKAction)
viewController.present(alertController, animated: true, completion: nil)
}
default:
if complete != nil {
complete!()
}
}
}
}
///
fileprivate func hasLocationService() -> Bool {
return CLLocationManager.locationServicesEnabled()
}
/// APP
fileprivate func hasLocationPermission() -> Bool {
switch locationPermission() {
case .notDetermined, .restricted, .denied:
return false
case .authorizedWhenInUse, .authorizedAlways:
return true
default:
break
}
return false
}
///
fileprivate func locationPermission() -> CLAuthorizationStatus {
if #available(iOS 14.0, *) {
let status: CLAuthorizationStatus = locationManager.authorizationStatus
print("location authorizationStatus is \(status.rawValue)")
return status
} else {
let status = CLLocationManager.authorizationStatus()
print("location authorizationStatus is \(status.rawValue)")
return status
}
}
//MARK: - didChangeAuthorization
func requestLocationAuthorizaiton() {
locationManager.requestWhenInUseAuthorization()
}
//MARK: -
func requestLocation() -> String {
//
locationManager.requestLocation()
return location
}
}
extension MP_LocationManager: CLLocationManagerDelegate {
//MARK: - ios 14.0
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
handleChangedAuthorization()
}
//MARK: - ios 14.0
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
handleChangedAuthorization()
}
private func handleChangedAuthorization() {
if let block = getAuthHandle, locationPermission() != .notDetermined {
if hasLocationPermission() {
block(true)
} else {
block(false)
}
}
}
//MARK: -
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let loction = locations.last {
//
print("latitude: \(loction.coordinate.latitude) longitude:\(loction.coordinate.longitude)")
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(loction) { (placemarks, error) in
if let placemark = placemarks?.first {
//
if let area = placemark.administrativeArea, !area.isEmpty {
print("Area code: \(area)")
self.location = area
} else if let locality = placemark.locality, !locality.isEmpty {
print("Area code: \(locality)")
self.location = locality
} else if let country = placemark.country, !country.isEmpty {
print("Country code: \(country)")
self.location = country
}
}
if let error = error {
print("Reverse geocoding failure: \(error.localizedDescription)")
//US
self.location = "US"
return
}
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Reverse geocoding failure: \(error.localizedDescription)")
//US
self.location = "US"
}
}