Skip to content

[iOS] Rebind accessibility bridge after view controller changes#187167

Open
smocer wants to merge 1 commit into
flutter:masterfrom
smocer:ios-accessibility-bridge-reattach
Open

[iOS] Rebind accessibility bridge after view controller changes#187167
smocer wants to merge 1 commit into
flutter:masterfrom
smocer:ios-accessibility-bridge-reattach

Conversation

@smocer
Copy link
Copy Markdown

@smocer smocer commented May 27, 2026

Keep the existing AccessibilityBridge when PlatformViewIOS detaches from an owner FlutterViewController, and rebind it when a controller/view is attached again.

In add-to-app integrations that reuse a FlutterEngine, the engine can detach from one FlutterViewController and later attach to another one. When semantics are already enabled, clearing the bridge during detach drops the UIKit accessibility representation of the current semantics tree. The Flutter UI continues to render, but VoiceOver / Accessibility Inspector can no longer see Flutter fields and buttons after reattachment.

This change preserves the bridge across owner-controller detach/reattach, skips semantics updates while no owner controller is attached, and rebinds the bridge to the new owner controller/view before semantics updates resume.

Fixes #186582

Pre-launch Checklist

@smocer smocer requested a review from a team as a code owner May 27, 2026 12:16
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 27, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@github-actions github-actions Bot added platform-ios iOS applications specifically engine flutter/engine related. See also e: labels. a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) team-ios Owned by iOS platform team labels May 27, 2026
@smocer smocer marked this pull request as draft May 27, 2026 12:17
@smocer smocer force-pushed the ios-accessibility-bridge-reattach branch from 551517b to 59d8bdb Compare May 27, 2026 18:04
@smocer smocer marked this pull request as ready for review May 27, 2026 18:07
@smocer
Copy link
Copy Markdown
Author

smocer commented May 27, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enables the iOS accessibility bridge to be rebound to sequential view controllers and views, ensuring that native accessibility elements like scroll views are correctly reattached. The review feedback highlights a critical issue where semantics updates could be discarded while the view controller is detached, leading to state desynchronization, and suggests an optimization for weak pointer copies and direct instance variable access in SemanticsObject.mm.

Comment on lines +158 to +163
if (!accessibility_bridge_ || !owner_controller_ || !owner_controller_.isViewLoaded) {
return;
}
accessibility_bridge_.get()->UpdateSemantics(std::move(update), actions);
[[NSNotificationCenter defaultCenter] postNotificationName:FlutterSemanticsUpdateNotification
object:owner_controller_];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Returning early when owner_controller_ is nil or !owner_controller_.isViewLoaded will completely discard any semantics updates sent by the Flutter framework while the view controller is detached or before its view is loaded. This causes the AccessibilityBridge's internal objects_ cache to become out of sync with the Flutter framework's actual semantics tree. When the view controller is later reattached or its view is loaded, the bridge will populate the view with stale semantics data.

Since AccessibilityBridge::UpdateSemantics is already safe to call when view_controller_ or view_ is nil (it will update the internal objects_ cache but skip updating the non-existent view or posting notifications), we should always forward the updates to the bridge if it exists, and only guard the NSNotificationCenter post.

  if (!accessibility_bridge_) {
    return;
  }
  accessibility_bridge_->UpdateSemantics(std::move(update), actions);
  if (owner_controller_ && owner_controller_.isViewLoaded) {
    [[NSNotificationCenter defaultCenter] postNotificationName:FlutterSemanticsUpdateNotification
                                                        object:owner_controller_];
  }
References
  1. Avoid duplicating state and keep only one source of truth by ensuring the accessibility bridge's internal cache is always kept in sync with the Flutter framework's semantics tree. (link)

Comment on lines +186 to +192
UIView* view = [self isAccessibilityBridgeAlive] ? self.bridge->viewIfLoaded() : nil;
if (self.scrollView.superview != view) {
[self.scrollView removeFromSuperview];
if (view) {
[view addSubview:self.scrollView];
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

We can optimize this method by avoiding redundant copies of the weak pointer self.bridge and bypassing the isAccessibilityBridgeAlive helper (which also copies the weak pointer). Additionally, accessing the backing instance variable _scrollView directly is more efficient and idiomatic in Objective-C than using the property getter self.scrollView within the class implementation.

  auto bridge = self.bridge;
  UIView* view = bridge ? bridge->viewIfLoaded() : nil;
  if (_scrollView.superview != view) {
    [_scrollView removeFromSuperview];
    if (view) {
      [view addSubview:_scrollView];
    }
  }
References
  1. Optimize for readability and performance by avoiding redundant weak pointer copies and using direct instance variable access. (link)

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the iOS platform view and accessibility bridge to support rebinding the accessibility bridge to sequential FlutterViewController and FlutterView instances when the owner controller is reattached. The reviewer identified an issue where skipping semantics updates while the view controller is detached or not loaded can cause the internal semantics tree to become out of sync. They recommend always forwarding semantics updates to the AccessibilityBridge and only guarding the notification post, as well as updating the corresponding unit tests to assert that updates are preserved while detached.

Comment on lines +157 to +163
UpdateAccessibilityBridgeViewController();
if (!accessibility_bridge_ || !owner_controller_ || !owner_controller_.isViewLoaded) {
return;
}
accessibility_bridge_.get()->UpdateSemantics(std::move(update), actions);
[[NSNotificationCenter defaultCenter] postNotificationName:FlutterSemanticsUpdateNotification
object:owner_controller_];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Skipping semantics updates when the view controller is detached or not loaded will cause the AccessibilityBridge's internal semantics tree to become out of sync and stale. Since the Flutter framework only sends incremental semantics updates, any updates occurring during this period will be permanently lost, leading to incorrect accessibility information once a view controller is reattached or loaded.

Instead, we should always forward the semantics updates to the AccessibilityBridge so it can maintain an up-to-date internal model. The AccessibilityBridge::UpdateSemantics method is already designed to safely handle a nil view controller (it updates its internal objects and returns early before posting notifications or updating elements). We should only guard the NSNotificationCenter notification post.

  UpdateAccessibilityBridgeViewController();
  if (!accessibility_bridge_) {
    return;
  }
  accessibility_bridge_.get()->UpdateSemantics(std::move(update), actions);
  if (owner_controller_ && owner_controller_.isViewLoaded) {
    [[NSNotificationCenter defaultCenter] postNotificationName:FlutterSemanticsUpdateNotification
                                                        object:owner_controller_];
  }

Comment on lines +213 to +279
- (void)testDoesNotUpdateSemanticsWhileOwnerControllerIsDetached {
flutter::MockDelegate mock_delegate;
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
/*platform=*/thread_task_runner,
/*raster=*/thread_task_runner,
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
id engine = OCMClassMock([FlutterEngine class]);

id firstViewController = OCMClassMock([FlutterViewController class]);
id secondViewController = OCMClassMock([FlutterViewController class]);
UIView* firstView = [[UIView alloc] init];
UIView* secondView = [[UIView alloc] init];

OCMStub([firstViewController isViewLoaded]).andReturn(YES);
OCMStub([firstViewController engine]).andReturn(engine);
OCMStub([firstViewController view]).andReturn(firstView);
OCMStub([firstViewController viewIfLoaded]).andReturn(firstView);
OCMStub([secondViewController isViewLoaded]).andReturn(YES);
OCMStub([secondViewController engine]).andReturn(engine);
OCMStub([secondViewController view]).andReturn(secondView);
OCMStub([secondViewController viewIfLoaded]).andReturn(secondView);
OCMStub([engine binaryMessenger]).andReturn(messenger);

auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
/*rendering_api=*/flutter::IOSRenderingAPI::kMetal,
/*platform_views_controller=*/nil,
/*task_runners=*/runners,
/*worker_task_runner=*/nil,
/*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
platform_view->SetOwnerViewController(firstViewController);
platform_view->SetSemanticsTreeEnabled(true);
flutter::AccessibilityBridge* bridge = platform_view->GetAccessibilityBridge();
XCTAssertTrue(bridge != nullptr);

flutter::SemanticsNode root_node;
root_node.id = kRootNodeId;
root_node.label = "root";
flutter::SemanticsNodeUpdates initial_update;
initial_update[kRootNodeId] = root_node;
platform_view->UpdateSemantics(/*view_id=*/0, std::move(initial_update),
flutter::CustomAccessibilityActionUpdates());
XCTAssertNotNil(firstView.accessibilityElements);

platform_view->SetOwnerViewController(nil);

flutter::SemanticsNode detached_root_node;
detached_root_node.id = kRootNodeId;
detached_root_node.label = "updated while detached";
flutter::SemanticsNodeUpdates detached_update;
detached_update[kRootNodeId] = detached_root_node;
platform_view->UpdateSemantics(/*view_id=*/0, std::move(detached_update),
flutter::CustomAccessibilityActionUpdates());
XCTAssertEqual(platform_view->GetAccessibilityBridge(), bridge);
XCTAssertNil(firstView.accessibilityElements);

platform_view->SetOwnerViewController(secondViewController);
XCTAssertEqual(platform_view->GetAccessibilityBridge(), bridge);
XCTAssertNotNil(secondView.accessibilityElements);
platform_view->SetSemanticsTreeEnabled(false);

[engine stopMocking];
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since we should preserve and apply semantics updates even while the view controller is detached, this test should be renamed to testPreservesSemanticsUpdatesWhileOwnerControllerIsDetached and updated to assert that the semantics update is indeed preserved and applied to the new view controller once it is attached.

- (void)testPreservesSemanticsUpdatesWhileOwnerControllerIsDetached {
  flutter::MockDelegate mock_delegate;
  fml::MessageLoop::EnsureInitializedForCurrentThread();
  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
                               /*platform=*/thread_task_runner,
                               /*raster=*/thread_task_runner,
                               /*ui=*/thread_task_runner,
                               /*io=*/thread_task_runner);
  id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
  id engine = OCMClassMock([FlutterEngine class]);

  id firstViewController = OCMClassMock([FlutterViewController class]);
  id secondViewController = OCMClassMock([FlutterViewController class]);
  UIView* firstView = [[UIView alloc] init];
  UIView* secondView = [[UIView alloc] init];

  OCMStub([firstViewController isViewLoaded]).andReturn(YES);
  OCMStub([firstViewController engine]).andReturn(engine);
  OCMStub([firstViewController view]).andReturn(firstView);
  OCMStub([firstViewController viewIfLoaded]).andReturn(firstView);
  OCMStub([secondViewController isViewLoaded]).andReturn(YES);
  OCMStub([secondViewController engine]).andReturn(engine);
  OCMStub([secondViewController view]).andReturn(secondView);
  OCMStub([secondViewController viewIfLoaded]).andReturn(secondView);
  OCMStub([engine binaryMessenger]).andReturn(messenger);

  auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
      /*delegate=*/mock_delegate,
      /*rendering_api=*/flutter::IOSRenderingAPI::kMetal,
      /*platform_views_controller=*/nil,
      /*task_runners=*/runners,
      /*worker_task_runner=*/nil,
      /*is_gpu_disabled_sync_switch=*/std::make_shared<fml::SyncSwitch>());
  platform_view->SetOwnerViewController(firstViewController);
  platform_view->SetSemanticsTreeEnabled(true);
  flutter::AccessibilityBridge* bridge = platform_view->GetAccessibilityBridge();
  XCTAssertTrue(bridge != nullptr);

  flutter::SemanticsNode root_node;
  root_node.id = kRootNodeId;
  root_node.label = "root";
  flutter::SemanticsNodeUpdates initial_update;
  initial_update[kRootNodeId] = root_node;
  platform_view->UpdateSemantics(/*view_id=*/0, std::move(initial_update),
                                 flutter::CustomAccessibilityActionUpdates());
  XCTAssertNotNil(firstView.accessibilityElements);

  platform_view->SetOwnerViewController(nil);

  flutter::SemanticsNode detached_root_node;
  detached_root_node.id = kRootNodeId;
  detached_root_node.label = "updated while detached";
  flutter::SemanticsNodeUpdates detached_update;
  detached_update[kRootNodeId] = detached_root_node;
  platform_view->UpdateSemantics(/*view_id=*/0, std::move(detached_update),
                                 flutter::CustomAccessibilityActionUpdates());
  XCTAssertEqual(platform_view->GetAccessibilityBridge(), bridge);
  XCTAssertNil(firstView.accessibilityElements);

  platform_view->SetOwnerViewController(secondViewController);
  XCTAssertEqual(platform_view->GetAccessibilityBridge(), bridge);
  XCTAssertNotNil(secondView.accessibilityElements);
  id rootElement = secondView.accessibilityElements.firstObject;
  XCTAssertEqualObjects([rootElement accessibilityLabel], @"updated while detached");
  platform_view->SetSemanticsTreeEnabled(false);

  [engine stopMocking];
}

Keep the existing AccessibilityBridge when PlatformViewIOS detaches from an owner FlutterViewController, and rebind it when a controller/view is attached again. This preserves the semantics tree after engines are reused across FlutterViewController instances.

Skip semantics updates while no owner controller is attached because UIKit accessibility elements need a valid container.

Add iOS unit coverage for detached semantics updates and reattaching a new owner controller.
@smocer smocer force-pushed the ios-accessibility-bridge-reattach branch from 59d8bdb to 43441f8 Compare May 27, 2026 18:47
@smocer
Copy link
Copy Markdown
Author

smocer commented May 27, 2026

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the iOS accessibility bridge lifecycle to support rebinding to sequential FlutterViewController and FlutterView instances. Instead of resetting the accessibility bridge when a view controller is detached, the bridge is preserved and updated with the new view controller via the newly introduced SetViewController method. This change ensures that semantics updates are preserved while detached and that native UIKit views (like scroll views) are correctly reattached to the active view. Comprehensive unit tests have been added to verify these rebinding behaviors and prevent regressions. I have no additional feedback to provide as there are no review comments.

@LouiseHsu LouiseHsu added the CICD Run CI/CD label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS] Accessibility tree is lost after reusing a FlutterEngine with a new FlutterViewController

2 participants