Skip to content

Instantly share code, notes, and snippets.

@goodmorningcody
Last active March 3, 2020 07:12
Show Gist options
  • Save goodmorningcody/74cc0465c680747df21b to your computer and use it in GitHub Desktop.
Save goodmorningcody/74cc0465c680747df21b to your computer and use it in GitHub Desktop.
스위프트 : 위치정보 사용 권한에 따른 예외처리 추가하기
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
// 중략
@IBOutlet var requestWeatherButton : UIButton!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
requestWeatherButton.enabled = false
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.Denied || status == CLAuthorizationStatus.Restricted {
let alert = UIAlertController(title: "위치정보", message: "설정에서 위치정보 사용을 위해 권한을 허용해주세요", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "확인", style: UIAlertActionStyle.Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
else if status == CLAuthorizationStatus.AuthorizedWhenInUse {
requestWeatherButton.enabled = true
}
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
requestWeatherButton.enabled = true
}
}
// 생략
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment