简体   繁体   中英

How does one call a function from ViewController in AppDelegate?

I am making an app that uses iBeacon. When the iBeacon is detected (inside AppDelegate), the app detects the phone's coordinates and forwards them to an API.

I have a function called runGPS() set up in ViewController which does the above and am trying to get it to execute when the iBeacon is detected.

When I do ViewController().runGPS(), I get an error: Missing parameter for 'coder' in call

I've looked up this issue and keep going around in circles on resolving it. One correction leads to another error etc. etc. etc.

How does one correct this or is my overall strategy for this wrong?

func locationManager(manager: CLLocationManager,
    didEnterRegion region: CLRegion) {
        manager.startRangingBeaconsInRegion(region as! CLBeaconRegion)
        manager.startUpdatingLocation()

        NSLog("You entered the region")

        ViewController(NSCoder)!.runGPS()

}

I have also implemented iBeacon in my application. You have to add this location manager delegate in that viewcontroller and set delegate there like

locationManager.delegate = self
locationManager.requestAlwaysAuthorization()

and you can check in viewController if any user enter to your beacon region then this delegate method will run

 func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {

    }

you can check your beacon in beacons array received in parameter.

You should separate out the runGPS() code from the ViewController class if its not meant to be there. Is it using any of the ViewController's functionality? If not then you should move it to a separate class meant for passing data to API. If you cannot do that then you can declare runGPS as a static method and called ViewController.runGPS() provided you are not using any of the properties of the ViewController in runGPS() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM