๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] ์•ฑ์‚ฌ์šฉ์ค‘ ์œ„์น˜ ๊ถŒํ•œ ๋ฐ›์•„์˜ค๊ณ  ํ˜„์žฌ ์œ„์น˜ ์•Œ์•„๋‚ด๊ธฐ ( Request Location Authorization & Detect Current Location)

by Fomagran ๐Ÿ’ป 2021. 2. 19.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

์˜ค๋Š˜ ์•Œ์•„๋ณผ๊ฑด ์ง€๋‚œ ์‹œ๊ฐ„์— ์ด์–ด์„œ ๋‚ ์”จ์— ๊ด€๋ จ๋œ ์ •๋ณด์ธ๋ฐ์š”.

 

ํ˜„์žฌ ๋‚ด๊ฐ€ ์žˆ๋Š” ์ง€์—ญ์— ๋‚ ์”จ๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„  ํ˜„์žฌ ์œ„์น˜๋ฅผ ์•Œ์•„์•ผ ํ•˜์ž–์•„์š”?

 

ํ˜„์žฌ ์œ„์น˜๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„  ์‚ฌ์šฉ์ž์˜ ๋™์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ ์‚ฌ์šฉ์ž์˜ ์œ„์น˜ ๊ถŒํ•œ ๋ฐ›๊ธฐ์ž…๋‹ˆ๋‹ค!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”!


Info.plist

 

๊ฐ€์žฅ ๋จผ์ € info.plist์—์„œ ์•ฑ์„ ์‚ฌ์šฉํ•˜๋Š”๋™์•ˆ ์œ„์น˜๋ฅผ ๋ฐ›์•„๋„ ๋˜๋ƒ๋Š” ๊ถŒํ•œ์„ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

	<key>NSLocationWhenInUseUsageDescription</key>
	<string>์œ„์น˜ ์ข€ ์“ธ๊ฒŒ?</string>

์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค!

 

๊ทธ๋ฆฌ๊ณ  ๋ฐ”๋กœ ViewController๋กœ ๊ฐ€์ค๋‹ˆ๋‹ค. (ํŽธ์˜์ƒ WeatherViewController๋ผ๊ณ  ๋ถ€๋ฅผ๊ฒŒ์š”!)


WeatherViewController

 

๋จผ์ € CoreLocation์„ import ํ•ด์ค๋‹ˆ๋‹ค.

import CoreLocation

๊ทธ๋ฆฌ๊ณค ์•„๋ž˜์™€ ๊ฐ™์ด locationManager์™€ currentLocation์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”!

    var locationManager:CLLocationManager?
    var currentLocation:CLLocationCoordinate2D!

์œ„์—์„œ CLLocationManager๋Š” ์• ํ”Œ ๋ฌธ์„œ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•ด์„ํ•˜๋ฉด "์•ฑ์—์„œ ์œ„์น˜ ๊ด€๋ จ ์ด๋ฒคํŠธ ์ „๋‹ฌ์„ ์‹œ์ž‘ํ•˜๊ณ  ์ค‘์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ์ฒด" ๋ผ๊ณ  ๋˜์–ด์žˆ๋„ค์š”.

 

๊ทธ๋ฆฌ๊ณ  CLLocationCoordinate2D๋Š” ์• ํ”Œ ๋ฌธ์„œ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•ด์„ํ•ด๋ณด๋ฉด ์œ„๋„์™€ ๊ฒฝ๋„๋ฅผ ์•Œ๋ ค์ฃผ๋Š” struct๋ผ๊ณ  ๋˜์–ด์žˆ๋„ค์š”

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๊ถŒํ•œ์„ ์š”์ฒญํ•ด๋ณผ๊นŒ์š”?

 

๋จผ์ € ์œ„์—์„œ ์„ธํŒ…ํ•œ locationManager๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์—†๋‹ค๋ฉด ๊ถŒํ•œ์„ ์š”์ฒญํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๋งŒ ๋ฐ”๋€Œ๊ณ  ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

    private func requestAuthorization() {
        if locationManager == nil {
            locationManager = CLLocationManager()
            //์ •ํ™•๋„๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค.
            locationManager!.desiredAccuracy = kCLLocationAccuracyBest
            //์•ฑ์„ ์‚ฌ์šฉํ• ๋•Œ ๊ถŒํ•œ์š”์ฒญ
            locationManager!.requestWhenInUseAuthorization()
            locationManager!.delegate = self
            locationManagerDidChangeAuthorization(locationManager!)
        }else{
            //์‚ฌ์šฉ์ž์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์†Œ๋“œ
            locationManager!.startMonitoringSignificantLocationChanges()
        }
    }
}

์œ„์—์„œ locationManager!.desiredAccuracy ๊ฐ€ ์žˆ๋Š”๋ฐ์š”.

 

์ด๊ฒƒ์€ ์–ผ๋งˆ๋‚˜ ์ •ํ™•ํ•˜๊ฒŒ ์œ„์น˜๋ฅผ ๋ฐ›๊ณ ์‹ถ๋ƒ๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  kCLLocationAccuracyBest ๊ฐ€ ์žˆ๋Š”๋ฐ์š”.

 

์ด๊ฑด ์•„๋ž˜์™€ ๊ฐ™์ด kCLLocationAccuracyBest.kCLLocationAccuracyKilometer,kCLLocationAccuracyThreeKilometers๋“ฑ ๋‹ค์–‘ํ•˜๊ฒŒ

 

์žˆ๋Š”๋ฐ ์ด๊ฑด ํ‚ฌ๋กœ๋ฏธํ„ฐ ๊ธฐ์ค€์œผ๋กœ ์œ„์น˜๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ํ• ์ง€ 3ํ‚ฌ๋กœ๋ฏธํ„ฐ ๊ธฐ์ค€์œผ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ํ• ์ง€ ์•„๋‹ˆ๋ฉด ์ตœ๊ณ ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ํ• ์ง€๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ฃผ์˜ํ•˜์‹ค ์ ์€ ์ •ํ™•ํ•˜๊ฒŒ ํ•˜๋ฉด ํ• ์ˆ˜๋ก ๋ฐฐํ„ฐ๋ฆฌ๊ฐ€ ๋นจ๋ฆฌ ์†Œ๋ชจ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

locationManager!.startMonitoringSignificantLocationChanges() ์€ ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

 locationManagerDidChangeAuthorization(locationManager!)๋Š” ๊ถŒํ•œ์ด ๋ฐ”๋€Œ์—ˆ๋Š”์ง€ ์ฒดํฌํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  CLLocationManagerDelegate ๋ฅผ ์ฑ„ํƒํ•ด์„œ ์“ฐ์…”์•ผํ•˜๋Š”๋ฐ์š”.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ฑ„ํƒํ•ด์ฃผ์‹  ๋’ค manager์˜ ํ˜„์žฌ ๊ถŒํ•œ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ณ  ๋งŒ์•ฝ ๊ถŒํ•œ์ด ํ—ˆ์šฉ๋˜์—ˆ๋‹ค๋ฉด

 

๋งจ ์ฒ˜์Œ ๋งŒ๋“ค์–ด์ฃผ์…จ๋˜ currentLocation์— locationManager!.location?.coordinate๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฒฝ๋„์™€ ์œ„๋„๋ฅผ ์•Œ์•„๋‚ด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

extension WeatherViewController:CLLocationManagerDelegate {
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        if manager.authorizationStatus == .authorizedWhenInUse {
            currentLocation = locationManager!.location?.coordinate
            LocationService.shared.longitude = currentLocation.longitude
            LocationService.shared.latitude = currentLocation.latitude
        }
    }
}

LocationService

 

๋กœ์ผ€์ด์…˜ ์„œ๋น„์Šค๋Š” ๊ฒฝ๋„์™€ ์œ„๋„๋ฅผ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

 

๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์ €์žฅํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!!

class LocationService {
    
    static var shared = LocationService()
    var longitude:Double!
    var latitude:Double!
  
}

์ด๋ ‡๊ฒŒ ๋ชจ๋‘ ์„ธํŒ…ํ•ด์ฃผ์‹  ๋’ค requestAuthorization() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ฃผ์‹œ๋ฉด

    override func viewDidLoad() {
        super.viewDidLoad()
        requestAuthorization()
    }

์•„๋ž˜์™€ ๊ฐ™์ด ๊ถŒํ•œ์„ค์ •์ด ๋‚˜์˜ค๊ณ 

 

ํ˜„์žฌ ๊ฒฝ๋„์™€ ์œ„๋„๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ์•Œ์•„๋‚ธ ๊ฒฝ๋„์™€ ์œ„๋„๋ฅผ ๊ฐ€์ง€๊ณ  ๋„์‹œ๋ฅผ ํŒŒ์•…ํ•ด๋ณด๋‹ˆ Seoul์ด๋ผ๊ณ  ์ž˜ ๋‚˜์˜ค๋„ค์š”!

 


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์œ„์น˜ ๊ถŒํ•œ ์„ค์ •๊ณผ ํ˜„์žฌ ์œ„์น˜ ์•Œ์•„๋‚ด๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ์ง€์ ํ•ด์ฃผ์‹ค ๋ถ€๋ถ„์ด๋‚˜ ๊ถ๊ธˆํ•˜์‹ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€