Search code examples
iosobjective-cswiftgpswindev

Develop in IOS without using Global Variable and class


I am developing an extra GPS module for an app in WinDev. WinDev supports Swift code but only method which excluding class and global variable use. Here what I've done (basically based on Apple doc) :

    import Foundation
    import CoreLocation
    import UIKit

    class mod_GPS : NSObject, CLLocationManagerDelegate {
        var distance = Double()
        var location_last : [CLLocation] = []
        let locationManager = CLLocationManager()

        func startReceivingLocationChanges() {
          // Do some stuff (basically what is write on doc)
        }

        internal func locationManager(_ manager: CLLocationManager,  didUpdateLocations locations: [CLLocation]) {
            let lastLocation = locations.last!
            if (location_last.count != 0){
                distance = distance + lastLocation.distance(from: location_last[0])
                location_last[0] = lastLocation
            }else{
                distance = 0
                location_last.append(lastLocation)
            }
        }

        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
           //Same
        }

    }

So here we have some code to create an object and count the distance when the user move. But this can't deal with WinDev. The issue is about : "let locationManager = CLLocationManager()" which have to be global. So I was wondering if there is a way to avoid global variable.

First I try something like that : the part in swift write on disk at every call of LocationManage(didUpdateLocation) and the part in WinDev read the value when he wanted. But this don't solve the problem because I still have to initiate the variable locationManager in WinDev part. Then I think about anonymous class which exist in Java but it seems to be missing in swift and objective C.

Moreover the community of WinDev developer and the documentation is totally AFK so I didn't expected help this way. I clearly don't know how to do this part. If someone have and idea or a tips... I'll be glad to hear them.


Solution

  • You could do something like this:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let locationManager = CLLocationManager()
        locationManager.delegate = self
        perform(#selector(hold), with: locationManager, afterDelay: 10000)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    
        NSObject.cancelPreviousPerformRequests(withTarget: self)
    }
    
    @objc func hold(object: AnyObject) {
        perform(#selector(hold), with: object, afterDelay: 10000)
    }
    

    Things to note:

    • CLLocationManager delegate is unowned, that is, it won't be set to nil when view controller is deallocated, you should make sure location manager will be deallocated before view controller does, or your app will crash if location manager lived more than its delegate (the view controller) and tried to access it.

    • You should make sure that all objects are deallocated when you are done with them using memory graph debugger or Xcode profiling instruments, else there will be memory leaks.

    Above code handles theses notes, but you should check them if you made any modifications.