How can I make Flutter run a different Target for iOS that is not the default "Runner"?
That's going to be tricky. If you look at the output of flutter run --help
command, you will see that it supports a custom --flavor
option that allows you to specify a custom scheme.
However, several things need to be done in order to make it work:
Open your workspace in Xcode - run open ios/Runner.xcworkspace
in the terminal, from your app's root.
Clone the Runner target by expanding the project and target list, clicking on the Runner project and selecting Duplicate (more details here).
This should create a custom scheme for you as well, with its own Info.plist file. The scheme will be called Runner-copy by default, rename it to what you named your new target (e.g. Staging).
Duplicate your debug and release build configurations and name them the way Flutter expects them to be named. For example, if your new target is called "Staging", you need to create a Debug-Staging and Release-Staging build configurations (more details on doing this).
Edit the Podfile and copy the entire target 'Runner' do
section, replacing the name of the target with yours. Afterwards, run pod install
.
Now that you have two different targets, you can do things like set different bundle ids, or include different files.
Run your custom scheme from the command line. For example: flutter run --flavor Staging
.
If step #5 failed, re-run pod install
manually, open the workspace in Xcode and run from there.
Note: this is pretty fragile, use at your own risk
Note: I was not able to get this to run in release mode
I recently had to do this for my Flutter project so I figured I'd add my process here.
If you have "Autocreate Schemes" checked off in your Scheme Manager, a new scheme will be created with the same name as the target you just created.
For each new target, add this to your podfile:
target '{TARGET_NAME}' do
use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
In the AppDelegate.swift file of your new target, paste the following:
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
}
}
On each target, you can now specify a unique bundle identifier.
This is especially useful if you're setting up a CI/CD pipeline for multiple environments since the xcodebuild command line tool let's you specify the target you want to use.
xcodebuild -target <your_target_name>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With