107107 flutter_view_controller_.reset ([flutter_view_controller retain ]);
108108}
109109
110+ UIViewController* FlutterPlatformViewsController::getFlutterViewController () {
111+ return flutter_view_controller_.get ();
112+ }
113+
110114void FlutterPlatformViewsController::OnMethodCall (FlutterMethodCall* call, FlutterResult& result) {
111115 if ([[call method ] isEqualToString: @" create" ]) {
112116 OnCreate (call, result);
161165
162166 FlutterTouchInterceptingView* touch_interceptor = [[[FlutterTouchInterceptingView alloc ]
163167 initWithEmbeddedView: embedded_view.view
164- flutterViewController: flutter_view_controller_. get ()
168+ platformViewsController: GetWeakPtr ()
165169 gestureRecognizersBlockingPolicy: gesture_recognizers_blocking_policies[viewType]]
166170 autorelease ];
167171
@@ -701,15 +705,17 @@ - (instancetype)initWithTarget:(id)target
701705// directly to the FlutterView.
702706@interface ForwardingGestureRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
703707- (instancetype )initWithTarget : (id )target
704- flutterViewController : (UIViewController*)flutterViewController ;
708+ platformViewsController :
709+ (fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController ;
705710@end
706711
707712@implementation FlutterTouchInterceptingView {
708713 fml::scoped_nsobject<DelayingGestureRecognizer> _delayingRecognizer;
709714 FlutterPlatformViewGestureRecognizersBlockingPolicy _blockingPolicy;
710715}
711716- (instancetype )initWithEmbeddedView : (UIView*)embeddedView
712- flutterViewController : (UIViewController*)flutterViewController
717+ platformViewsController :
718+ (fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController
713719 gestureRecognizersBlockingPolicy :
714720 (FlutterPlatformViewGestureRecognizersBlockingPolicy)blockingPolicy {
715721 self = [super initWithFrame: embeddedView.frame];
@@ -720,9 +726,9 @@ - (instancetype)initWithEmbeddedView:(UIView*)embeddedView
720726
721727 [self addSubview: embeddedView];
722728
723- ForwardingGestureRecognizer* forwardingRecognizer =
724- [[[ForwardingGestureRecognizer alloc ] initWithTarget: self
725- flutterViewController: flutterViewController ] autorelease ];
729+ ForwardingGestureRecognizer* forwardingRecognizer = [[[ForwardingGestureRecognizer alloc ]
730+ initWithTarget: self
731+ platformViewsController:std: : move (platformViewsController) ] autorelease ];
726732
727733 _delayingRecognizer.reset ([[DelayingGestureRecognizer alloc ]
728734 initWithTarget: self
@@ -833,39 +839,42 @@ - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
833839@end
834840
835841@implementation ForwardingGestureRecognizer {
836- // We can't dispatch events to the framework without this back pointer.
837- // This is a weak reference, the ForwardingGestureRecognizer is owned by the
838- // FlutterTouchInterceptingView which is strong referenced only by the FlutterView,
839- // which is strongly referenced by the FlutterViewController.
840- // So this is safe as when FlutterView is deallocated the reference to ForwardingGestureRecognizer
841- // will go away.
842- UIViewController* _flutterViewController;
842+ // Weak reference to FlutterPlatformViewsController. The FlutterPlatformViewsController has
843+ // a reference to the FlutterViewController, where we can dispatch pointer events to.
844+ //
845+ // The lifecycle of FlutterPlatformViewsController is bind to FlutterEngine, which should always
846+ // outlives the FlutterViewController. And ForwardingGestureRecognizer is owned by a subview of
847+ // FlutterView, so the ForwardingGestureRecognizer never out lives FlutterViewController.
848+ // Therefore, `_platformViewsController` should never be nullptr.
849+ fml::WeakPtr<flutter::FlutterPlatformViewsController> _platformViewsController;
843850 // Counting the pointers that has started in one touch sequence.
844851 NSInteger _currentTouchPointersCount;
845852}
846853
847854- (instancetype )initWithTarget : (id )target
848- flutterViewController : (UIViewController*)flutterViewController {
855+ platformViewsController :
856+ (fml::WeakPtr<flutter::FlutterPlatformViewsController>)platformViewsController {
849857 self = [super initWithTarget: target action: nil ];
850858 if (self) {
851859 self.delegate = self;
852- _flutterViewController = flutterViewController;
860+ FML_DCHECK (platformViewsController.get () != nullptr );
861+ _platformViewsController = std::move (platformViewsController);
853862 _currentTouchPointersCount = 0 ;
854863 }
855864 return self;
856865}
857866
858867- (void )touchesBegan : (NSSet *)touches withEvent : (UIEvent*)event {
859- [_flutterViewController touchesBegan: touches withEvent: event];
868+ [_platformViewsController. get ()-> getFlutterViewController () touchesBegan: touches withEvent: event];
860869 _currentTouchPointersCount += touches.count ;
861870}
862871
863872- (void )touchesMoved : (NSSet *)touches withEvent : (UIEvent*)event {
864- [_flutterViewController touchesMoved: touches withEvent: event];
873+ [_platformViewsController. get ()-> getFlutterViewController () touchesMoved: touches withEvent: event];
865874}
866875
867876- (void )touchesEnded : (NSSet *)touches withEvent : (UIEvent*)event {
868- [_flutterViewController touchesEnded: touches withEvent: event];
877+ [_platformViewsController. get ()-> getFlutterViewController () touchesEnded: touches withEvent: event];
869878 _currentTouchPointersCount -= touches.count ;
870879 // Touches in one touch sequence are sent to the touchesEnded method separately if different
871880 // fingers stop touching the screen at different time. So one touchesEnded method triggering does
@@ -877,7 +886,8 @@ - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
877886}
878887
879888- (void )touchesCancelled : (NSSet *)touches withEvent : (UIEvent*)event {
880- [_flutterViewController touchesCancelled: touches withEvent: event];
889+ [_platformViewsController.get ()->getFlutterViewController () touchesCancelled: touches
890+ withEvent: event];
881891 _currentTouchPointersCount = 0 ;
882892 self.state = UIGestureRecognizerStateFailed;
883893}
0 commit comments