Skip to content

Commit 90cdbe5

Browse files
committed
[iOS] Migrate @UIApplicationMain attribute to @main
1 parent c845a78 commit 90cdbe5

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import '../../base/file_system.dart';
6+
import '../../base/project_migrator.dart';
7+
import '../../xcode_project.dart';
8+
9+
const String _appDelegateFileBefore = r'''
10+
@UIApplicationMain
11+
@objc class AppDelegate: FlutterAppDelegate {
12+
''';
13+
14+
const String _appDelegateFileAfter = r'''
15+
@main
16+
@objc class AppDelegate: FlutterAppDelegate {
17+
''';
18+
19+
/// Replace the deprecated `@UIApplicationMain` attribute with `@main`.
20+
///
21+
/// See:
22+
/// https://github.com/apple/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md
23+
class UIApplicationmMainDeprecationMigration extends ProjectMigrator {
24+
UIApplicationmMainDeprecationMigration(
25+
IosProject project,
26+
super.logger,
27+
) : _appDelegateSwift = project.appDelegateSwift;
28+
29+
final File _appDelegateSwift;
30+
31+
@override
32+
Future<bool> migrate() async {
33+
// Skip this migration if the project uses Objective-C.
34+
if (!_appDelegateSwift.existsSync()) {
35+
return true;
36+
}
37+
38+
// Migrate the ios/Runner/AppDelegate.swift file.
39+
final String original = _appDelegateSwift.readAsStringSync();
40+
final String migrated = original.replaceFirst(_appDelegateFileBefore, _appDelegateFileAfter);
41+
if (original == migrated) {
42+
return true;
43+
}
44+
45+
logger.printStatus(
46+
'ios/Runner/AppDelegate.swift does not use the @main attribute, updating.',
47+
);
48+
_appDelegateSwift.writeAsStringSync(migrated);
49+
return true;
50+
}
51+
}

packages/flutter_tools/lib/src/xcode_project.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ class IosProject extends XcodeBasedProject {
178178

179179
File get appFrameworkInfoPlist => _flutterLibRoot.childDirectory('Flutter').childFile('AppFrameworkInfo.plist');
180180

181+
/// The 'AppDelegate.swift' file of the host app. This file might not exist if the app project uses Objective-C.
182+
File get appDelegateSwift => _editableDirectory.childDirectory('Runner').childFile('AppDelegate.swift');
183+
181184
File get infoPlist => _editableDirectory.childDirectory('Runner').childFile('Info.plist');
182185

183186
Directory get symlinks => _flutterLibRoot.childDirectory('.symlinks');
184187

185-
/// True, if the app project is using swift.
186-
bool get isSwift {
187-
final File appDelegateSwift = _editableDirectory.childDirectory('Runner').childFile('AppDelegate.swift');
188-
return appDelegateSwift.existsSync();
189-
}
188+
/// True if the app project uses Swift.
189+
bool get isSwift => appDelegateSwift.existsSync();
190190

191191
/// Do all plugins support arm64 simulators to run natively on an ARM Mac?
192192
Future<bool> pluginsSupportArmSimulator() async {

packages/flutter_tools/templates/app_shared/ios-swift.tmpl/Runner/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Flutter
22
import UIKit
33

4-
@UIApplicationMain
4+
@main
55
@objc class AppDelegate: FlutterAppDelegate {
66
override func application(
77
_ application: UIApplication,

0 commit comments

Comments
 (0)