133 lines
4.6 KiB
Swift
133 lines
4.6 KiB
Swift
//
|
|
// CommonUtils.swift
|
|
// WebAppUIKitBase
|
|
//
|
|
// Created by Sean Kim on 10/21/24.
|
|
//
|
|
|
|
import UIKit
|
|
import CoreLocation
|
|
|
|
import Alamofire
|
|
|
|
class CommonUtils: NSObject, CLLocationManagerDelegate {
|
|
static let shared: CommonUtils = CommonUtils()
|
|
|
|
private override init() {}
|
|
|
|
//MARK: - Location
|
|
weak var locationDelegate: LocationDelegate?
|
|
|
|
let locationManager: CLLocationManager = {
|
|
let manager = CLLocationManager()
|
|
manager.desiredAccuracy = kCLLocationAccuracyBest
|
|
manager.distanceFilter = kCLDistanceFilterNone
|
|
return manager
|
|
}()
|
|
var lastLocation: CLLocation?
|
|
|
|
|
|
public func checkLocationPermission() {
|
|
printLog("[위치] Check Loc Permission")
|
|
locationManager.delegate = self
|
|
locationManager.requestWhenInUseAuthorization()
|
|
self.toggleUpdatingLocation(true)
|
|
}
|
|
|
|
public func toggleUpdatingLocation(_ toggle: Bool) {
|
|
if toggle {
|
|
locationManager.startUpdatingLocation()
|
|
} else {
|
|
locationManager.stopUpdatingLocation()
|
|
}
|
|
}
|
|
|
|
// 14이상인 경우만 산정해서 한거라서 14.0 미만을 원하면 다른 메서드를 만들어야 함
|
|
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
|
printLog("[위치] locationManagerDidChangeAuthorization") // 처음 불러 올 떄 요거 불려짐
|
|
switch manager.authorizationStatus {
|
|
case .authorizedAlways, .authorizedWhenInUse, .authorized:
|
|
printLog("[위치] 권한 허용")
|
|
self.locationDelegate?.checkPermission(true)
|
|
self.toggleUpdatingLocation(true)
|
|
default:
|
|
printLog("[위치] 권한 미허용 & 기타 오류")
|
|
self.locationDelegate?.checkPermission(false)
|
|
self.toggleUpdatingLocation(false)
|
|
|
|
}
|
|
}
|
|
|
|
// 14이상인 경우만 산정해서 한거라서 14.0 미만을 원하면 다른 메서드를 만들어야 함
|
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
printLog("[위치] didUpdateLocations") // 요건 값을 가져올 때 불려짐
|
|
switch manager.authorizationStatus {
|
|
case .authorizedAlways, .authorizedWhenInUse:
|
|
printLog("[위치] 권한 허용")
|
|
self.locationDelegate?.checkPermission(true)
|
|
if let location = locations.first{
|
|
printLog("[위치] 정보 수신 성공")
|
|
self.locationDelegate?.getLocation(location)
|
|
}
|
|
default:
|
|
printLog("[위치] 권한 미허용 & 기타 오류")
|
|
self.locationDelegate?.checkPermission(false)
|
|
}
|
|
}
|
|
|
|
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
|
printLog("[위치] didFailWithError")
|
|
self.locationDelegate?.checkPermission(false)
|
|
}
|
|
|
|
|
|
// MARK: - URL
|
|
func afGET(url: String, param: [String:String], headers: HTTPHeaders?) async throws -> Any {
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
AF.request(url,
|
|
method: .get,
|
|
parameters: param,
|
|
headers: headers)
|
|
.validate(statusCode: 200 ..< 300)
|
|
.responseString { response in
|
|
printLog("[LOG] response: \(response)")
|
|
switch response.result {
|
|
case .success(let value):
|
|
printLog("[LOG]")
|
|
continuation.resume(returning: value)
|
|
case .failure(let error):
|
|
printLog("[LOG]")
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public func jsonToDict(_ input: String) -> [String: Any] {
|
|
if let jsonData = input.data(using: .utf8){
|
|
do {
|
|
if let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
|
|
return jsonObject
|
|
}
|
|
} catch let error { // 이 부분
|
|
printLog("JSON ERROR: \(error))")
|
|
}
|
|
}
|
|
return [:]
|
|
}
|
|
|
|
public func jsonToType<T: Decodable>(_ input: String, as type: T.Type) -> T? {
|
|
if let jsonData = input.data(using: .utf8) {
|
|
do {
|
|
let decodedObject = try JSONDecoder().decode(T.self, from: jsonData)
|
|
return decodedObject
|
|
} catch let error {
|
|
printLog("JSON 디코딩 오류: \(error)")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
}
|
|
|