How to enable Swift support for existing project in flutter

天大地大妈咪最大 提交于 2019-12-03 23:36:08

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, if you don't have any change in files inside of this folder(like info.plist, appDelegate, Podfile or Splash Screens), you can delete it.
  2. Run the command below in terminal/cmd, on root folder of your proyect...
    -i swift is for swift
    -a kotlin is for kotlin (You don't need delete anything before)
    You can use only one of these or both, (Don't forget the period "." at the end of the command)
    flutter create -i swift -a kotlin .
  1. *Apply again your changes on the iOS folder (aka info.plist), now in Runner folder, now you don't have main.m and AppDelegate.h files, instead you have AppDelegate.swift in swift language, so if you need to put APIs there the code is different.

  2. *If you apply -a kotlin line, check your previosly changes on files in Android folder(example: verify the AndroidX migration code in build.gradle - gradle.properties, verify the manifest), because the command add some code or change completely some files.

    Also if you want, you can delete the android/app/src/main/java folder because now the app use kotlin folder

  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.

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)

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

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.

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

Serge Shkurko

First you need to get the AppDelegate.swift file You can do this by generating a new project flutter create -i swift The file will be located along the path: ios/Runner/AppDelegate.swift Or, copy the code below and create a file with the same name (taken from a new project for flutter 1.9.x)

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)
  }
}

Next, delete the files from ios/Runner : AppDelegate.m, AppDelegate.h, main.m (if he exist). Then we go into xcode and delete these files in it.

In the file manager, copies the AppDelegate.swift file to ios/Runner. Open the ios/Runner folder Next, take and drag the file AppDelegate.swift into ios/Runner. Drag and drop only to xcode!

Mark the indicated items, after click finish

Create a bridge

Run flutter clean, pod install (in ios folder) one by one. And run using flutter run

Done!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!