-2

I know how to do shake gesture and how to find location separately, however I don't know how to find location after the user shakes the device. I have xcode version 6.4.

Right now I have a motion ended function. And a showUserLocation function.
I would like to put a statement in motionEnded that calls the showUserLocation so that the location is found if the user shakes the phone.

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {

      ?????  

    }

// Showing the user location
func showUserLocation(sender : AnyObject) {
    let status = CLLocationManager.authorizationStatus()

    //Asking for authorization to display current location
    if status == CLAuthorizationStatus.NotDetermined {
        locationManager.requestWhenInUseAuthorization()
    } else {
        locationManager.startUpdatingLocation()
    }
  • This question is too broad and the answer is a simple answer though may be hard to implement as is almost always the case in software development, after the phone shakes, in you your code, you know the shake has happened, now find the location – Brian Ogden Mar 26 '17 at 20:35
  • More specifically: what goes in these brackets override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { ???? } That connects to the showUserLocation function I have set up – Alicia Lebby Mar 26 '17 at 20:39
  • You cannot ask questions like, posting code in comments is too hard for anyone to understand and give you an answer, what goes into the brackets is any code you want, in some form or syntax, you can call a function of yours if it is accessible, in scope, on the right thread – Brian Ogden Mar 26 '17 at 20:44
  • Brian, I appreciate you taking the time to help me on this. I am looking for a more specific answer. I will edit my question now to hopefully make it more clear. – Alicia Lebby Mar 26 '17 at 20:58

1 Answers1

0

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()
    }
}
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176