본문 바로가기

프로그래밍/Flutter

[flutter] ios 기본 모듈 구성 방식

Integrate a Flutter module into your iOS project | Flutter

 

Integrate a Flutter module into your iOS project

Learn how to integrate a Flutter module into your existing iOS project.

docs.flutter.dev

 

모듈 만들기

cd some/path/
flutter create --template module my_module

 

기본 구성요소

Runner xcode 프로젝트와 워크스페이스 : ./.ios (숨김폴더)
dart 소스 : ./lib
테스트 : ./test
flutter 프로젝트 정보 : ./pubspec.yaml

 

./.ios 폴더의 경우 자동 생성 및 수정이 일어나므로, 저장소에서는 제외되어야 하며, 개발환경 변경시 flutter pub get 명령으로 .ios 폴더를 다시 생성할 수 있다.

./lib 폴더에 모듈을 작성합니다.

 

모듈을 실행하거나 빌드

flutter run --debug
flutter build ios-framework --output=path/

 

build/ios/framework/Debug, Profile, Release 하위에
Flutter.xcframework 와
Dart 코드가 빌드된 App.xcframework 가 각각 생성됨.

플러그인이 포함된 경우
FlutterPluginRegistrant.xcframework와 my_plugin.xcframework 가 포함


기존 어플리케이션에 flutter 모듈 추가

A: 코코아팟을 통해 모듈 추가

개발자들이 flutter sdk 와 동일한 sdk path를 사용해야 하며, 앱 컴파일시에 모듈의 컴파일도 같이 수행된다.

프로젝트 구성
some/path/my_app
some/path/my_module


앱의 Podfile 수정

flutter_module_path = '../my_module'
load File.join(flutter_module_path, '.ios', 'Flutter', 'podhelper.rb')

target 'MyApp' do
  install_all_flutter_pods(flutter_module_path)
end

post_install do |installer|
   flutter_post_install(installer) if defined?(flutter_post_install)
end

pod install


B: XCode에서 모듈 추가

모듈을 빌드해 xcframework를 xcode에서 포함해 사용하는 방식
flutter sdk를 사용하지 않는 개발환경에도 앱 빌드가 가능

모듈을 앱 하위에 빌드

flutter build ios-framework --output=some/path/my_app/Flutter

 

Build Phases > Link Binary With Libraries
특정 configuration path의 모든 xcframework  등록
my_app.xcodeproj/project.pbxproj 파일을 편집기에서 열어 추가한 프레임워크의 path 를 변경
path = "Flutter/$(CONFIGURATION)/App.xcframework"

Build Settings > Framework Search Paths
타겟별 path 설정 $(PROJECT_DIR)/Flutter/$(CONFIGURATION)

General > Frameworks and Libraries
App.xcframework, Flutter.xcframework 등 사용할 프레임워크 추가

 

C: Flutter.xcframework만 cocoapod 으로 배포

모듈을 Flutter.xcframework 제외하고 빌드

flutter build ios-framework --cocoapods --output=some/path/my_app/Flutter

Flutter.xcframework 가 Flutter.podspec 으로 변경됨

앱의 Podfile 에서 추가

pod 'Flutter', :podspec => 'some/path/my_app/Flutter/{build mode}/Flutter.podspec'

build mode 에 따라 사용할 수 있는 명령들이 달라지므로, 필요에 따라 선택

나머지 프레임워크들은 B 방식으로 추가