pmb pmb - 3 years ago 119
Swift Question

How do I pass params on timer selector

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let mostRecentLocation = locations.last else {

Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(StartTestVC.sendDataToServer), userInfo: nil, repeats: true)

func sendDataToServer (latitude: Double, longitude: Double) {
SFUserManager.shared.uploadPULocation(latitude, longitude:longitude)

I want send data to server every 1 minute. I am using Timer.scheduledTimer and setting selector. But how could I send lat/lng params to my function?

Answer Source

For sending the data with Timer you can use the userInfo parameter for pass the data.

Here is the sample by which you can get call of selector method and by that you can pass your location coordinate to it.

Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(iGotCall(sender:)), userInfo: ["Name": "i am iOS guy"], repeats:true)

For handling that userInfo you need to go according to below.

func iGotCall(sender: Timer) {

for your case make sure your didUpdateLocations is called frequently.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download