Skip to content

Instantly share code, notes, and snippets.

@thiagogabriel
Created October 6, 2014 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagogabriel/d0120547565c91089b72 to your computer and use it in GitHub Desktop.
Save thiagogabriel/d0120547565c91089b72 to your computer and use it in GitHub Desktop.
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var locationStatus = CLLocationManager.authorizationStatus()
@IBOutlet weak var buttonNext: UIButton!
@IBAction func openPlacesView(sender: AnyObject) {
askForLocation(sender)
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
setTitleForNextButton()
}
func askForLocation(sender: AnyObject) {
if (locationStatus == CLAuthorizationStatus.Denied) {
let url = NSURL.URLWithString(UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(url)
} else if (locationStatus != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
} else if (locationStatus == CLAuthorizationStatus.AuthorizedWhenInUse){
self.performSegueWithIdentifier("seguePlaceViewController", sender: sender)
}
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
setTitleForNextButton()
}
private
func setTitleForNextButton() {
locationStatus = CLLocationManager.authorizationStatus()
buttonNext.titleLabel?.textAlignment = NSTextAlignment.Center
var text = "This text is shown in the button."
if (locationStatus == CLAuthorizationStatus.Denied) {
text = "Please, we need your location."
} else if (locationStatus != CLAuthorizationStatus.AuthorizedWhenInUse) {
text = "Give us your location to list the places around you"
} else if (locationStatus == CLAuthorizationStatus.AuthorizedWhenInUse){
text = "Let's go! :)"
}
buttonNext.setTitle(text, forState: UIControlState.Normal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment