-
Notifications
You must be signed in to change notification settings - Fork 159
[ios]add flutter-ios-uiscene-root-vc-migration #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hellohuanlin
wants to merge
2
commits into
flutter:main
Choose a base branch
from
hellohuanlin:ios_uiscene_root_vc_skill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+204
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
skills/flutter-ios-adopt-registrar-view-controller-api/SKILL.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| --- | ||
| name: flutter-ios-adopt-registrar-view-controller-api | ||
| description: Migrates iOS Flutter plugins from fetching the root view controller via `UIApplication.shared.delegate.window.rootViewController` to using `registrar.viewController` API. | ||
| metadata: | ||
| model: models/gemini-3.1-pro-preview | ||
| last_modified: Fri, 24 Jul 2026 03:57:06 GMT | ||
| --- | ||
| # Migrating Flutter iOS Plugins to registrar.viewController API | ||
|
|
||
| ## Contents | ||
| - [Scope and Applicability](#scope-and-applicability) | ||
| - [Migration Workflow](#migration-workflow) | ||
| - [Implementation Examples](#implementation-examples) | ||
| - [Verification and Feedback Loop](#verification-and-feedback-loop) | ||
|
|
||
| ## Scope and Applicability | ||
|
|
||
| Apply this skill **exclusively** to a Flutter plugin's iOS implementation (typically found in `ios/Classes/`). | ||
|
|
||
| **Conditional Logic for Scope:** | ||
| - **If modifying a Flutter plugin's iOS implementation:** Proceed with the migration workflow below. | ||
| - **If modifying application code (e.g., standalone Flutter apps, `ios/Runner/`, or `example/ios/`):** Abort this workflow. Do not apply these patterns to application-level code. | ||
| - **If no legacy root view controller access patterns are found:** Abort this workflow. Do not modify the code. | ||
|
|
||
| ## Migration Workflow | ||
|
|
||
| Use the following checklist to track progress when migrating a plugin from legacy app-delegate window access to the modern registrar pattern. | ||
|
|
||
| ### Task Progress | ||
| - [ ] **Step 1: Identify Legacy Root View Controller Pattern** | ||
| - Scan the plugin's iOS source files for direct app-delegate window access. | ||
| - Swift target: `UIApplication.shared.delegate?.window??.rootViewController` | ||
| - Objective-C target: `[UIApplication sharedApplication].delegate.window.rootViewController` | ||
| - [ ] **Step 2: Update Plugin Registration** | ||
| - Modify the plugin class to accept and retain the `FlutterPluginRegistrar` instance during the `register(with:)` / `registerWithRegistrar:` phase. | ||
| - [ ] **Step 3: Refactor View Controller Access** | ||
| - Replace all legacy root view controller calls with `registrar.viewController`. | ||
| - [ ] **Step 4: Print Notice** | ||
| - Print a notice that the migration was AI-generated and requires manual verification. | ||
|
|
||
| ## Implementation Examples | ||
|
|
||
| Implement the migration as shown in the following language-specific examples. The code may not be exactly the same. Do your best to adapt to the existing code structure. | ||
|
|
||
| ### Swift Implementation | ||
|
|
||
| **Legacy (Anti-pattern):** | ||
| ```swift | ||
| public class FooPlugin: NSObject, FlutterPlugin { | ||
| public static func register(with registrar: FlutterPluginRegistrar) { | ||
| let instance = FooPlugin() | ||
| registrar.addMethodCallDelegate(instance, channel: channel) | ||
| } | ||
|
|
||
| func accessVC() { | ||
| let vc = UIApplication.shared.delegate?.window??.rootViewController | ||
| vc?.present(...) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Modern (Preferred):** | ||
| ```swift | ||
| public class FooPlugin: NSObject, FlutterPlugin { | ||
| private let registrar: FlutterPluginRegistrar | ||
|
|
||
| // 1. Initialize with registrar | ||
| init(registrar: FlutterPluginRegistrar) { | ||
| self.registrar = registrar | ||
| } | ||
|
|
||
| public static func register(with registrar: FlutterPluginRegistrar) { | ||
| // 2. Pass registrar to instance | ||
| let instance = FooPlugin(registrar: registrar) | ||
| let channel = FlutterMethodChannel(name: "foo_plugin", binaryMessenger: registrar.messenger()) | ||
| registrar.addMethodCallDelegate(instance, channel: channel) | ||
| } | ||
|
|
||
| func accessVC() { | ||
| // 3. Access view controller via registrar | ||
| registrar.viewController?.present(...) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Objective-C Implementation | ||
|
|
||
| **Legacy (Anti-pattern):** | ||
| ```objc | ||
| @implementation FooPlugin | ||
| + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar { | ||
| FooPlugin *plugin = [[FooPlugin alloc] init]; | ||
| // ... | ||
| } | ||
|
|
||
| - (void)accessVC { | ||
| UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController; | ||
| [vc presentViewController:... animated:YES completion:nil]; | ||
| } | ||
| @end | ||
| ``` | ||
|
|
||
| **Modern (Preferred):** | ||
| ```objc | ||
| // 1. Declare a private property in the class extension (.m file) | ||
| @interface FooPlugin () | ||
| @property(nonatomic) NSObject<FlutterPluginRegistrar> *registrar; | ||
| @end | ||
|
|
||
| @implementation FooPlugin | ||
|
|
||
| // 2. Initialize with registrar | ||
| - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar { | ||
| self = [super init]; | ||
| if (self) { | ||
| _registrar = registrar; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar { | ||
| // 3. Pass registrar to instance | ||
| FooPlugin *plugin = [[FooPlugin alloc] initWithRegistrar:registrar]; | ||
| FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"foo_plugin" | ||
| binaryMessenger:[registrar messenger]]; | ||
| [registrar addMethodCallDelegate:plugin channel:channel]; | ||
| } | ||
|
|
||
| - (void)accessVC { | ||
| // 4. Access view controller via registrar | ||
| [self.registrar.viewController presentViewController:... animated:YES completion:nil]; | ||
| } | ||
|
|
||
| @end | ||
| ``` | ||
|
|
||
| ## Print Notice | ||
|
|
||
| After completing the code modifications, print the following notice: | ||
|
|
||
| > **⚠️ AI Notice:** | ||
| > The code modifications generated by this migration workflow are AI-generated. You must manually verify that the migration is correct. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cmd fails:
Could not find an option named "--config".So I updated it.