투케이2K

168. (ios/swift) CLLocationManager 사용해 실시간 위치 위도 , 경도 확인 실시 - latitude , longitude 본문

IOS

168. (ios/swift) CLLocationManager 사용해 실시간 위치 위도 , 경도 확인 실시 - latitude , longitude

투케이2K 2022. 8. 17. 15:43
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT

 

[사전 설정]

 

[소스 코드]

import UIKit

// MARK: [import 선언 실시]
import CoreLocation


// MARK: [클래스 CLLocationManagerDelegate 추가]
class ViewController: UIViewController, CLLocationManagerDelegate {
    
    
    // MARK: [뷰 메모리 로드 수행 실시]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print("")
        print("===============================")
        print("[ViewController >> viewDidLoad() :: 뷰 메모리 로드 실시]")
        print("===============================")
        print("")
        
        
        // [테스트 함수 호출 실시]
        self.testMain()
        
    }
    
    
    
    
    
    // MARK: - [뷰 화면 표시]
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("")
        print("===============================")
        print("[ViewController >> viewDidAppear() :: 뷰 화면 표시]")
        print("===============================")
        print("")
    }
    
    
    
    
    
    // MARK: - [뷰 종료 상태]
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        print("")
        print("===============================")
        print("[ViewController >> viewDidDisappear() :: 뷰 종료 상태]")
        print("===============================")
        print("")
    }

    
    
    
    
    
    // MARK: - [테스트 함수 정의]
    var locationManager : CLLocationManager! // [위치 권한 사용 매니저]
    func testMain() {
        print("")
        print("===============================")
        print("[ViewController >> testMain() :: 테스트 함수 수행]")
        print("===============================")
        print("")
        
        self.locationManager = CLLocationManager.init() // locationManager 초기화
        self.locationManager.delegate = self // 델리게이트 넣어줌
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest // 거리 정확도 설정
        self.locationManager.requestAlwaysAuthorization() // 위치 권한 설정 값을 받아옵니다

        self.locationManager.startUpdatingLocation() // 위치 업데이트 시작
    }
    
    
    
    
    
    // MARK: - [위치 서비스에 대한 권한 확인 실시]
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways {
            print("")
            print("===============================")
            print("[ViewController > locationManager() : 위치 사용 권한 항상 허용]")
            print("===============================")
            print("")
        }
        if status == .authorizedWhenInUse {
            print("")
            print("===============================")
            print("[ViewController > locationManager() : 위치 사용 권한 앱 사용 시 허용]")
            print("===============================")
            print("")
        }
        if status == .denied {
            print("")
            print("===============================")
            print("[ViewController > locationManager() : 위치 사용 권한 거부]")
            print("===============================")
            print("")
        }
        if status == .restricted || status == .notDetermined {
            print("")
            print("===============================")
            print("[ViewController > locationManager() : 위치 사용 권한 대기 상태]")
            print("===============================")
            print("")
        }
    }
    
    
    
    
    
    // MARK: - [위치 정보 지속적 업데이트]
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            // [위치 정보가 nil 이 아닌 경우]
            print("")
            print("===============================")
            print("[ViewController > didUpdateLocations() : 위치 정보 확인 실시]")
            print("[위도 : \(location.coordinate.latitude)]")
            print("[경도 : \(location.coordinate.longitude)]")
            print("===============================")
            print("")
        }
    }
    
    
    
    
    
    // MARK: - [위도, 경도 받아오기 에러가 발생한 경우]
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("")
        print("===============================")
        print("[ViewController > didFailWithError() : 위치 정보 확인 에러]")
        print("[error : \(error)]")
        print("[localizedDescription : \(error.localizedDescription)]")
        print("===============================")
        print("")
    }



} // [클래스 종료]
 

[결과 출력]

 

 

반응형
Comments