In react native, I'm using NativeModules and RCTResponseSenderBlock to create a callback. Because the callback is specific to the function I declare in my bridge, I can't access it from the Extension that provides the feedback I need. This is built on the example code from the Mapbox Navigation iOS SDK.
I've tried various ways to reach the callback property from by extended class.
@objc(TbtNavigation)
class TbtNavigation: NSObject {
@objc
func takeMeToWH(_ callback: RCTResponseSenderBlock) {
let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House")
let options = NavigationRouteOptions(waypoints: [origin, destination])
Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first else { return }
let navigationViewController = NavigationViewController(for: route)
navigationViewController.delegate = self
let appDelegate = UIApplication.shared.delegate
appDelegate!.window!!.rootViewController!.present(navigationViewController, animated: true, completion: nil)
}
callback(["CALLBACK WORKS HERE"])
}
}
extension TbtNavigation: NavigationViewControllerDelegate {
// Show an alert when arriving at the waypoint and wait until the user to start next leg.
func navigationViewControllerDidDismiss(_ navigationViewController: NavigationViewController, byCanceling canceled: Bool) {
print("cancelled")
callback(["I NEED THE CALLBACK TO WORK HERE"])
let appDelegate = UIApplication.shared.delegate
appDelegate!.window!!.rootViewController!.dismiss(animated: true, completion: nil)
}
}
callback in navigationViewController is 'unresolved identifier'
EDIT: the solution was to declare the callback variable in scope of the class.
@objc(TbtNavigation)
class TbtNavigation: NSObject {
var callback: RCTResponseSenderBlock?
@objc
func takeMeToWH(_ callback: @escaping RCTResponseSenderBlock) {
let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.912605, longitude: -77.035402), name: "White House")
let options = NavigationRouteOptions(waypoints: [origin, destination])
Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first else { return }
let navigationViewController = NavigationViewController(for: route)
navigationViewController.delegate = self
let appDelegate = UIApplication.shared.delegate
appDelegate!.window!!.rootViewController!.present(navigationViewController, animated: true, completion: nil)
}
self.callback = callback
}
}
extension TbtNavigation: NavigationViewControllerDelegate {
// Show an alert when arriving at the waypoint and wait until the user to start next leg.
func navigationViewControllerDidDismiss(_ navigationViewController: NavigationViewController, byCanceling canceled: Bool) {
self.callback?([canceled])
let appDelegate = UIApplication.shared.delegate
appDelegate!.window!!.rootViewController!.dismiss(animated: true, completion: nil)
}
}