В реакции на натив, я использую NativeModules и RCTResponseSenderBlock для создания обратного вызова. Поскольку обратный вызов специфичен для функции, которую я объявляю в моем мосту, я не могу получить к ней доступ из Расширения, которое обеспечивает необходимую обратную связь. Это построено на примере кода из SDK для Mapbox Navigation.
Я пробовал разные способы получения свойства обратного вызова из расширенного класса.
@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)
}
}
обратный вызов в navigationViewController - это «неразрешенный идентификатор»
РЕДАКТИРОВАТЬ: решение состояло в том, чтобы объявить переменную обратного вызова в области видимости класса.
@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)
}
}