27

I want to know if there is a way to enable swift support for flutter project. I only enabled Kotlin support while creating the project. I need to enable Swift too. Is there a command I can execute or any setting in flutter plugin for Android studio where I can enable or is there is an option to enable in Xcode?

This is what I want to do but for existing Flutter project

This is what I want to do but for existing Flutter project

Shashi Kiran
  • 999
  • 5
  • 13
  • 27

6 Answers6

29
  1. Delete existing ios folder from root of flutter project.
  2. Run this command flutter create -i swift .

This command will create only ios directory with swift support.

Jawand Singh
  • 1,929
  • 1
  • 24
  • 21
  • 2
    deleting the iOS will also delete all the information associated with it. if you have been working on an app for long , this is not an option because you will have to start again from scratch – Dyary Jan 10 '20 at 20:34
  • 1
    @Dyary true indeed, you have to be careful if there is some custom code written in `/ios` project. But, I think we have git (assuming that's the practice we all use) for it and we can always find old code back from history. :) – Jawand Singh Jan 12 '20 at 11:02
  • 2
    Sure thing. I just wanted to notify others of the extra step of pulling from git again since you really don't need to delete the folders for no reason :) because that is what i did and had to rebuild my project – Dyary Jan 14 '20 at 19:53
22

Well, I search the same thing now, also I enable the kotlin support... so, how to enable swift or kotlin support for a existing proyect?

  1. For swift support, you need to move your ios folder to outside of your project folder, for kotlin move outside the android folder, also check your package name in your manifest, or your PRODUCT_BUNDLE_IDENTIFIER

  2. Run the below flutter command in your terminal on root folder of your proyect (I'm using com.custom_name.my_proyect for the package name of this example)

    • -i swift is for swift
    • -a kotlin is for kotlin.
    • --org is to set the first two words of your package, in this case com.custom_name
    • --project-name is to set the last word of your package in this case my_proyect

You can use only switf/kotlin or both, (Don't forget the period '.' at the end of the command)

    flutter create -i swift -a kotlin --org com.custom_name --project-name my_proyect .
  1. *Apply again your previous custom changes on the ios folder (E.g: info.plist, custom splash screen, etc.), now in Runner folder you don't have main.m and AppDelegate.h files, instead you have only the AppDelegate.swift file in swift language, so if you need to put API_KEYs there, the code is different.

  2. *If you apply -a kotlin line, is the same logic that swift in your android folder, so your MainActivy.java file is now a MainActivity.kt file in kotlin language, and you need to apply again your previous custom changes in Android folder (E.g: build.gradle, res folder, android_manifest, etc.).

Hector Aguero
  • 369
  • 3
  • 8
11

Little known secret -- you can run flutter create . in your Flutter app directory and it will repair the project, recreating any files that are missing. So if you already have a project created with Objective-C and Java, you can run:

flutter create -i swift -a kotlin .

to convert the host app to Kotlin and Swift.

(Ignore Kotlin you you want, but my experience is that just leave it there)

TruongSinh
  • 4,662
  • 32
  • 52
2

Bridging Header must be created. Open the project with XCode. Then choose File -> New -> File -> Swift File. A dialog will be displayed when creating the swift file(Since this file is deleted, any name can be used.). XCode will ask you if you wish to create Bridging Header, click yes. Make sure you have use_frameworks! in the Runner block, in ios/Podfile。 Make sure you have SWIFT_VERSION 4.2 selected in you XCode -> Build Settings Do flutter clean Go to your ios folder, delete Podfile.lock and Pods folder and then execute pod install --repo-update

Alberto Diaz
  • 113
  • 1
  • 1
  • 9
0

For the scenario where you already have an ios module partially written in Objective-C, and now just want to use Swift code alongside, than I suggest you to right click on ios in project window and choose Open iOS module in XCode this from context menu

context menu

Than you can just follow those instructions: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

If your plan is to rewrite module in swift than I'd create a new project with the same name as your original one and turned on Swift support. Than I'd just copy the whole ios module to your original project.

muminers
  • 1,190
  • 10
  • 19
0

First delete AppDelegate.h, AppDelegate.m, main.m files And also remove main.m from build phases -> compile sources

After that run this command flutter create -i swift .

vikram
  • 181
  • 2
  • 5