-
-
Save thiagogabriel/d0120547565c91089b72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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