웰코발
웰코's iOS
웰코발
전체 방문자
오늘
어제
  • 분류 전체보기 (63)
    • Swift (26)
    • rxSwift (13)
    • SwiftUI (3)
    • iOS (12)
    • 기타 (1)
    • 개발관련 용어정리 (6)
    • 면접준비 (0)
    • 공공데이터 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Coordinator
  • alamofire
  • content_available
  • swiftUI
  • rxswift
  • collectionview
  • delay
  • ios
  • SWIFT
  • Scroll
  • UI
  • uitableview
  • 대기오염통계 현황
  • 주제구독
  • Observable
  • 측정소정보
  • 디자인
  • WKWebView
  • cell
  • ReactorKit

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
웰코발

웰코's iOS

Swift

[Swift] CLLocationManager.locationServicesEnabled() Issue 문제

2022. 11. 2. 20:33

 

Xcode 14.0 업그레이드 후 iOS 16 기기에서 

CLLocationManager.locationServicesEnabled()

사용 후 문제점

This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationManagerDidChangeAuthorization: callback and checking authorizationStatus first.

 

 

해결방안

CLLocationManager.locationServicesEnabled() ->  CLLocationManager.authorizationStatus 활용

lazy var locationManager: CLLocationManager = {
        let manager = CLLocationManager()
        manager.desiredAccuracy = kCLLocationAccuracyBest
        return manager
    }()

class ..... {

    override func viewDidLoad() {

        // ...
    
        // if CLLocationManager.locationServicesEnabled() {   // 해당 코드 대신 하단 코드 사용
        if locationManager.authorizationStatus == .authorizedAlways || locationManager.authorizationStatus == .authorizedWhenInUse {
            print("viewdidload 위치 서비스 On 상태")
    
        } else {
            print("viewdidload 위치 서비스 Off 상태")

        }
    
        // ...
    
        checkLocationPermission()
    
    }

    override func didBecomeActive() {
        checkLocationPermission()
    }

    /*
        사용자의 위치 정보를 확인할 수 있도록 위치 권한을 확인한다.
     */
    func checkLocationPermission() {
        
        switch locationManager.authorizationStatus {
        case .authorizedAlways, .authorizedWhenInUse:
            // 위치 사용 권한이 허용되어 있음
            // ...
            locationManager.delegate = self
            // ...
        case .notDetermined:
            // 위치 사용 권한 허용 여부 미결정 상태. 권한을 허용할 것인지 여부를 묻는 팝업이 나타난다.
            // ...
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
            // ...
        case .restricted, .denied:
            // 위치 사용 권한이 허용되지 않은 상태.
            // ...
        @unknown default:
            // [개인정보보호] -> [위치 서비스] 활성화 X
            // ...
        }
        
        view.setNeedsLayout()
    }

}

/* extension for CLLocationManager */
extension IntegratedMapViewController : CLLocationManagerDelegate {

    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print("[locationManagerDidChangeAuthorization]")
        
        switch manager.authorizationStatus {
        case .authorizedAlways, .authorizedWhenInUse:
            // 사용자가 위치 권한 사용을 허용한 상태.
            print("CLLocationManagerDelegate - 사용자가 위치 권한 사용을 허용한 상태임")
            // ...
            
        case .denied, .restricted, .notDetermined:
            // 위치 사용 권한이 허용되지 않은 상태.
            print("CLLocationManagerDelegate - 사용자가 위치 권한 사용을 허용하지 않은 상태임")
            // ...
            
        @unknown default:
            // 알수없는 상태.
            // ... error 처리
            
        }

        
        view.setNeedsLayout()
    }
    
}

'Swift' 카테고리의 다른 글

[Swift] WebKit 의 웹 관련 캐시 삭제 하는 방법  (0) 2022.12.16
[Swift] Alamofire 통신에서 캐시 사용 안하는 방법  (0) 2022.11.25
[Swift] UIScrollView 내 UITextView 의 Cursor 로 focus scroll 하는 방법  (0) 2022.11.17
[Swift] ScrollView 에서 Button Highlight Delay가 생길 때  (2) 2022.11.11
[Swift] WebKit 의 WKWebView 사용법  (0) 2022.10.18
    'Swift' 카테고리의 다른 글
    • [Swift] Alamofire 통신에서 캐시 사용 안하는 방법
    • [Swift] UIScrollView 내 UITextView 의 Cursor 로 focus scroll 하는 방법
    • [Swift] ScrollView 에서 Button Highlight Delay가 생길 때
    • [Swift] WebKit 의 WKWebView 사용법
    웰코발
    웰코발
    나의 개발 일지

    티스토리툴바