Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make multiple iOS Targets in Flutter?

Tags:

ios

flutter

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

like image 241
Daniel Oliveira Avatar asked Oct 10 '18 18:10

Daniel Oliveira


2 Answers

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

like image 91
David Airapetyan Avatar answered Oct 16 '22 16:10

David Airapetyan


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>
like image 1
Joe Muller Avatar answered Oct 16 '22 15:10

Joe Muller