14

How can I make Flutter run a different Target for iOS that is not the default "Runner"?

Daniel Oliveira
  • 8,053
  • 11
  • 40
  • 76

2 Answers2

4

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:

  1. Open your workspace in Xcode - run open ios/Runner.xcworkspace in the terminal, from your app's root.

  2. 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).

  1. 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).

  2. 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.

  1. Run your custom scheme from the command line. For example: flutter run --flavor Staging.

  2. 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

David Airapetyan
  • 5,301
  • 4
  • 40
  • 62
  • This article is a good cross-platform guide to flavors: https://medium.com/@salvatoregiordanoo/flavoring-flutter-392aaa875f36 – David Airapetyan Nov 22 '18 at 19:22
  • A few more comments: 1. You might have to setup additional configurations by copying Flutter/Debug.xcconfig and Flutter/Release.xcconfig, then adding them to the project 2. With those, CocoaPods may complain - this has a good workaround: https://github.com/CocoaPods/CocoaPods/issues/2633 3. If building in Xcode, it may complain about missing /packages/flutter_tools/bin/xcode_backend.sh - if it's the case, set the FLUTTER_ROOT environment variable to point to the Flutter installation 4. Flutter expects the .app file to be called Runner.app - might have to deploy via ideviceinstaller – David Airapetyan Nov 27 '18 at 18:47
  • 2
    I have done this with similar steps but got "Could not find the built application bundle at build/ios/iphoneos/Runner.app.". Seems like flutter is using that as the default binary. – Panda World May 05 '19 at 13:27
  • Could you re-produce this from scratch and share your project? – David Airapetyan May 06 '19 at 15:28
  • I think the idea is that you keep the original Runner target and duplicate it 2 more times so that flutter stays happy. You should end up with 3 targets not two @Panda World. I think flutter needs the runner target to work. – The Senator Aug 09 '19 at 20:08
1

Answer for 2022

I recently had to do this for my Flutter project so I figured I'd add my process here.

Create a new Target

  1. In xcode, right-click on the "Runner" target
  2. Select "Duplicate"
  3. Rename the new target appropriately (Dev, Staging, Prod, etc)

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.

Create new Debug, Profile, and Release Configurations

  1. Click on "Runner" in the project navigator.
  2. Ensure the Runner PROJECT is selected, not the Runner TARGET.
  3. Click the Editor->Add Configuration->Duplicate "Debug" Configuration.
  4. Rename the new configuration to Debug-{Target} (replacing {Target} with your target name)
  5. Repeat for profile and release configurations

Update your podfile

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

Update AppDelegate.swift

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>
Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61
  • 1
    Thank you so much.i was stuck in this for last couple of days.. but after adding separate Runners it also added multiple info.plist files. named as info_copy.plist , info_copy2.plist etc. should i rename them or remove them? please suggest – abdulec90 May 23 '22 at 08:22
  • 1
    In last part you set `if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate }` why we even need that ... that not gona called you put it after `return` statement.. – Wahab Khan Jadon Jun 26 '22 at 07:21
  • Where do I find the `AppDelegate.swift` file for my new target? Creating a new target didn't make any new files or folders for me... – Nathan Tew Sep 26 '22 at 06:03