Your question has nothing to do with a shake motion ending or calling location. Your question is one of basic syntax of Swift and general understanding of any programming language.
You code implementation for getting a location was "boxed" into an action func, perhaps a UIButton touchup inside where a sender was required as an argument. But you do not use the sender at all for getting location information, so it is easy to move this implementation to its own function, showUserLocationHelper
so the action func can call getLocation and the shake motion can call get location.
The name of your action function: showUserLocation
is poorly named it should have a name like btnShowUserLocationTouchUpInside
, a long name but not confusing like what you named your action function and than tried to use in motionEnded
.
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
//you can't pass the sender so couldn't call your showUserLocation action function,
//so move implementation of showUserLocation to showUserLocationHelper and now you can do what you need after shake event
showUserLocationHelper()
}
// Showing the user location
//this is an action function for some UI element like a UIButton and the sender is required to be passed for this function to fire thus, motionEnded cannot directly call it
//but you do not use sender at all
func showUserLocation(sender : AnyObject) {
showUserLocationHelper()
}
func showUserLocationHelper() {
let status = CLLocationManager.authorizationStatus()
//Asking for authorization to display current location
if status == CLAuthorizationStatus.NotDetermined {
locationManager.requestWhenInUseAuthorization()
} else {
locationManager.startUpdatingLocation()
}
}