๐Ÿงฏ Troubleshooting

[SWM] CLLocationManagerDelegate - ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š” ๋ฉ”์„œ๋“œ

sani_ 2023. 2. 7. 22:07

 

  ๋ฌธ์ œ  

์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ์œ„์น˜๋ฅผ ๋ฐ›์•„์™€์„œ ์ง€๋„๋ฅผ ์ด๋™ํ•ด ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด, CLLocationManager๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค.

CLLocationManagerDelegate์˜ locationManagerDidChangeAuthorization ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๊ณ ์ž 

CLLocationManager ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ–ˆ๋Š”๋ฐ๋„, locationManagerDidChangeAuthorization ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š์•˜๋‹ค.

HomeViewController๊ฐ€ NavigationController์— embed๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์˜€๊ณ ,

final class HomeViewController: BaseViewController {
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
    }

extension HomeViewController: CLLocationManagerDelegate {
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
    	print(#function)
        checkUserDeviceLocationServiceAuthorization()
    }
}

์œ„์™€ ๊ฐ™์ด CLLocationManager ์ธ์Šคํ„ด์Šค์˜ delegate์— self๋ฅผ ํ• ๋‹นํ•ด๋„ 

locationManagerDidChangeAuthorization ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š์•˜๋‹ค.

 

 

๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ด๋„,

์ด๋ ‡๊ฒŒ CLLocationManager ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋Š”๋ฐ ๋ง์ด๋‹ค..

 

๋‹ค๋ฅธ iOS ๊ฐœ๋ฐœ์ž๋“ค ์‚ฌ์ด์—์„œ๋„ ๋ฒ„๊ทธ ํ˜น์€ ์•Œ ์ˆ˜ ์—†๋Š”(?) ๊ณต์‹๋ฌธ์„œ์— ์ ํ˜€ ์žˆ์ง€ ์•Š์€ ๋‹ค๋ฅธ ๋ฌด์–ธ๊ฐ€๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค๋Š” ๊ฒฐ๋ก ์ด ๋‚ฌ๋‹ค๋Š” ์ด์•ผ๊ธฐ๋„ ๋“ค์—ˆ๋‹ค.. ๊ทธ๋ฆฌ๊ณ  UINavigationController ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ UITabBarController์— embed๋˜์–ด ์žˆ์„ ๋•Œ๋„ ๋™์ผํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

 

 

 

  ํ•ด๊ฒฐ  

๋ฐฉ๋ฒ• 1. locationManagerDidChangeAuthorization ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•œ๋‹ค.

final class HomeViewController: BaseViewController {
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManagerDidChangeAuthorization(locationManager)
    }

extension HomeViewController: CLLocationManagerDelegate {
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print(#function)
        checkUserDeviceLocationServiceAuthorization()
    }
}

์œ„์ฒ˜๋Ÿผ CLLocationManagerDelegate์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ฐฉ๋ฒ• 2. CLLocationManager ์ธ์Šคํ„ด์Šค์˜ ์ดˆ๊ธฐํ™”๋ฅผ loadView() ํ˜น์€ ๊ทธ ์ดํ›„์— ํ•œ๋‹ค.

final class HomeViewController: BaseViewController {
    var locationManager: CLLocationManager!   // ํƒ€์ž…๋งŒ ์„ ์–ธ

    override func loadView() {
        view = homeView
        locationManager = CLLocationManager()  // loadView()์—์„œ ์ดˆ๊ธฐํ™” ํ˜น์€
    } 

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()  // viewDidLoad()์—์„œ ์ดˆ๊ธฐํ™”!
        locationManager.delegate = self
    }

extension HomeViewController: CLLocationManagerDelegate {
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print(#function)
        checkUserDeviceLocationServiceAuthorization()
    }
}

์œ„์ฒ˜๋Ÿผ CLLocationManager์˜ ์ดˆ๊ธฐํ™”๋ฅผ ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์„ ์–ธํ•  ๋•Œ ํ•˜์ง€ ์•Š๊ณ  loadView() ํ˜น์€ ๊ทธ ์ดํ›„์— ์‹คํ–‰ํ–ˆ์„ ๋•Œ locationManagerDidChangeAuthorization ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” viewDidLoad()์—์„œ๋งŒ ํ•ด ๋ณด๊ณ  ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋Š”๋ฐ, loadView()์—์„œ๋„ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค!

๊ทธ ์ดํ›„ ์‹œ์ ์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•„์š”์— ๋”ฐ๋ผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ๋‹น์—ฐํžˆ delegate ์ง€์ • ์ „์— ํ•ด์•ผํ•  ๊ฒƒ,, ์•ˆ ๊ทธ๋Ÿฌ๋ฉด locationManager.delegate = self ์—์„œ Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ๋งŒ๋‚  ๊ฒƒ์ด๋‹คโ€ฆ

 

1๋ฒˆ๊ณผ 2๋ฒˆ ๋ฐฉ๋ฒ• ์ค‘, ์ง์ ‘ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋ณด๋‹ค๋Š”, ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์‹œ์ ์˜ ๋ฌธ์ œ์ผ ์ˆ˜ ์žˆ์œผ๋‹ˆ 2๋ฒˆ์ด ๋” ์ ํ•ฉํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 


์ฐธ๊ณ ์‚ฌํ•ญ

locationManagerDidChangeAuthorization(_:)

CLLocationManager 

Assign the delegate immediately when you configure your location manager, because the system reports the appโ€™s authorization status to the delegateโ€™s locationManagerDidChangeAuthorization(_:) method after the location manager finishes initializing itself. Core Location calls the methods of your delegate object using the RunLoop of the thread on which you initialized the CLLocationManager object. That thread must itself have an active RunLoop, like the one found in your appโ€™s main thread.

CLLocationManager์— ์œ„์™€ ๊ฐ™์€ ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ, RunLoop๊ณผ embed์— ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š”์ง€ ๋” ์•Œ์•„๋ณด์•„์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค!

 

 

๋Œ“๊ธ€์ˆ˜0