[iOS] Migrate @UIApplicationMain attribute to @main (#146707)

This migrates Flutter to use the `@main` attribute introduced in Swift 5.3. The `@UIApplicationMain` attribute is deprecated and will be removed in Swift 6. See: https://github.com/apple/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md

This change is split into two commits:

1. ad18797428 - This updates the iOS app template and adds a migration to replace `@UIApplicationMain` uses with `@main`. 
2. 8ecbb2f29f - I ran `flutter run` on each Flutter iOS app in this repository to verify the app migrates and launches successfully.

Part of https://github.com/flutter/flutter/issues/143044
This commit is contained in:
Loïc Sharma
2024-04-16 15:13:03 -07:00
committed by GitHub
parent 5a0369df16
commit 194fefaa53
14 changed files with 173 additions and 20 deletions

View File

@@ -35,6 +35,7 @@ import 'migrations/project_base_configuration_migration.dart';
import 'migrations/project_build_location_migration.dart';
import 'migrations/remove_bitcode_migration.dart';
import 'migrations/remove_framework_link_and_embedding_migration.dart';
import 'migrations/uiapplicationmain_deprecation_migration.dart';
import 'migrations/xcode_build_system_migration.dart';
import 'xcode_build_settings.dart';
import 'xcodeproj.dart';
@@ -158,6 +159,7 @@ Future<XcodeBuildResult> buildXcodeProject({
XcodeScriptBuildPhaseMigration(app.project, globals.logger),
RemoveBitcodeMigration(app.project, globals.logger),
XcodeThinBinaryBuildPhaseInputPathsMigration(app.project, globals.logger),
UIApplicationMainDeprecationMigration(app.project, globals.logger),
];
final ProjectMigration migration = ProjectMigration(migrators);

View File

@@ -16,14 +16,12 @@ class RemoveBitcodeMigration extends ProjectMigrator {
final File _xcodeProjectInfoFile;
@override
Future<bool> migrate() async {
Future<void> migrate() async {
if (_xcodeProjectInfoFile.existsSync()) {
processFileLines(_xcodeProjectInfoFile);
} else {
logger.printTrace('Xcode project not found, skipping removing bitcode migration.');
}
return true;
}
@override

View File

@@ -0,0 +1,51 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../../base/file_system.dart';
import '../../base/project_migrator.dart';
import '../../xcode_project.dart';
const String _appDelegateFileBefore = r'''
@UIApplicationMain
@objc class AppDelegate''';
const String _appDelegateFileAfter = r'''
@main
@objc class AppDelegate''';
/// Replace the deprecated `@UIApplicationMain` attribute with `@main`.
///
/// See:
/// https://github.com/apple/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md
class UIApplicationMainDeprecationMigration extends ProjectMigrator {
UIApplicationMainDeprecationMigration(
IosProject project,
super.logger,
) : _appDelegateSwift = project.appDelegateSwift;
final File _appDelegateSwift;
@override
Future<void> migrate() async {
// Skip this migration if the project uses Objective-C.
if (!_appDelegateSwift.existsSync()) {
logger.printTrace(
'ios/Runner/AppDelegate.swift not found, skipping @main migration.',
);
return;
}
// Migrate the ios/Runner/AppDelegate.swift file.
final String original = _appDelegateSwift.readAsStringSync();
final String migrated = original.replaceFirst(_appDelegateFileBefore, _appDelegateFileAfter);
if (original == migrated) {
return;
}
logger.printWarning(
'ios/Runner/AppDelegate.swift uses the deprecated @UIApplicationMain attribute, updating.',
);
_appDelegateSwift.writeAsStringSync(migrated);
}
}

View File

@@ -178,15 +178,15 @@ class IosProject extends XcodeBasedProject {
File get appFrameworkInfoPlist => _flutterLibRoot.childDirectory('Flutter').childFile('AppFrameworkInfo.plist');
/// The 'AppDelegate.swift' file of the host app. This file might not exist if the app project uses Objective-C.
File get appDelegateSwift => _editableDirectory.childDirectory('Runner').childFile('AppDelegate.swift');
File get infoPlist => _editableDirectory.childDirectory('Runner').childFile('Info.plist');
Directory get symlinks => _flutterLibRoot.childDirectory('.symlinks');
/// True, if the app project is using swift.
bool get isSwift {
final File appDelegateSwift = _editableDirectory.childDirectory('Runner').childFile('AppDelegate.swift');
return appDelegateSwift.existsSync();
}
/// True if the app project uses Swift.
bool get isSwift => appDelegateSwift.existsSync();
/// Do all plugins support arm64 simulators to run natively on an ARM Mac?
Future<bool> pluginsSupportArmSimulator() async {