2022年5月にFlutter3がリリースされました。この記事ではFlutter2について解説しており、最新の情報ではない可能性があります。
ここではiOS実機上でFlutterアプリを動作させる手順について説明します。
Flutter環境の確認
説明が重複しますが、Flutter 環境構築 に書いたようにAndroid StudioとFlutterの環境を構築してください。
- Xcode と Cocoapods をインストールしてください。
- flutter doctor コマンドで正しい環境設定ができていることを確認してください。
- iOS Simulator の上でFlutterアプリが動作することを確認してください。
iOS実機用の環境設定
iOS実機をMacに接続すて正しく認識されると、Android Studioの [Flutter Device Selection] のプルダウンリストにデバイスが表示されるので選択します。
何もせずにプロジェクトを実行しようとすると以下のメッセージが出ますので、メッセージに従って順番に対処していきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
═══════════════════════════════════════════════════════════════════════════════════ No valid code signing certificates were found You can connect to your Apple Developer account by signing in with your Apple ID in Xcode and create an iOS Development Certificate as well as a Provisioning Profile for your project by: 1- Open the Flutter project's Xcode target with open ios/Runner.xcworkspace 2- Select the 'Runner' project in the navigator then the 'Runner' target in the project settings 3- In the 'General' tab, make sure a 'Development Team' is selected. You may need to - Log in with your Apple ID in Xcode first - Ensure you have a valid unique Bundle ID - Register your device with your Apple Developer Account - Let Xcode automatically provision a profile for your app 4- Build or run your project again 5- Trust your newly created Development Certificate on your iOS device via Settings > General > Device Management > [your new certificate] > Trust For more information, please visit: https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/MaintainingCertificates/MaintainingCertificates.html Or run on an iOS simulator without code signing ═══════════════════════════════════════════════════════════════════════════════════ |
- ターミナルでプロジェクトのフォルダを開きます。そこで open ios/Runner.xcworkspace と入力すると Xcode が起動します。
1 2 |
AndroidStudioProjects % cd flutter_app ←プロジェクトに移動する flutter_app % open ios/Runner.xcworkspace |
- 左上の [Runner] を選択し、[General] タブの中の [Bundle identifier] をユニークな名前に変更します。
- 開発用のアカウントを作成していない場合は新規に作成します[Xcode] > [Preferences…] から [Accounts] のタブを選択して、左下の「+」をクリックして Apple ID を追加します。アカウントを選択すると自動でプロビジョニングが始まり、Provisioning Profile と Signing Certificate の部分が埋まります。プロビジョニングファイルは数日で期限が切れますが、Xcodeを起動すると再生されます。
- Xcodeに戻り、[Signing & Capabilities] のタブの [Team] で開発用のアカウントを選択します。
- Xcode の実行ボタン(上部の▶)を押すと、キーチェーンのアクセスに関してダイアログが出てくるので Mac のログイン・パスワードを入力します。何度も聞かれるので、聞いてこなくなるまで「常に許可」にしてください。何度も同じダイアログが出てきます。
- Android Studio に戻ってからプロジェクトを実行します。これで実機上で起動するはずです。
エラー対策
iPhone 側で「システム環境設定」から「一般」を選択「デバイス管理」に新しく 4. で Team に指定したアカウントが追加されているので「信頼」にします。
Your device is locked. Unlock your device first before running.
Android Studioでビルドが完了しても、次のようなメッセージが表示されてインストールを失敗することがあります。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Launching lib/main.dart on iPad in debug mode... Signing iOS app for device deployment using developer identity: "Apple Development: ma@zi3.jp (6ZKZCF75AM)" Running Xcode build... Xcode build done. 22.6s Installing and launching... ═══════════════════════════════════════════════════════════════════════════════════ Your device is locked. Unlock your device first before running. ═══════════════════════════════════════════════════════════════════════════════════ Could not run build/ios/iphoneos/Runner.app on bbf702fabfcd57145b4afc81efdedebd12751e04. Try launching Xcode and selecting "Product > Run" to fix the problem: open ios/Runner.xcworkspace Error launching application on iPad. |
アプリのインストール時は、iOS実機がロックされていない状態である必要があります。iOS実機のロック画面を解除してください。
“iproxy”は、開発元を検証できないため開けません。
iOS実機にアプリをインストールする時に、Mac上に次のようなエラーメッセージが表示される場合があります。
このメッセージは [キャンセル] をクリックして閉じ、Macの「システム環境設定」から「セキュリティーとプライバシー」を開きます。
「”iproxy”は開発元を確認できないため、使用がブロックされました。」と表示されているので、[このまま許可] をクリックしてアプリのビルドを再実行します。
Firebase
Firebaseを使ったアプリの場合はFirebaseコンソールでプロジェクトを開き、iOSアプリを追加登録し、GoogleService-Info.plistをダウンロードしてください。GoogleService-Info.plistをXcodeでRunner追加し、Xcodeを実行すればビルド成功してアプリが起動するはずです。
iOS実機上で起動できるようにする
ここまでで、Android StudioまたはXcodeから起動したアプリがiOS実機にインストールされて動作したはずです。ところが、iOS実機に登録されたアプリアイコンをクリックしてもアプリは起動しません。
In iOS 14+, debug mode Flutter apps can only be launched from Flutter tooling, IDEs with Flutter plugins or from Xcode.
Alternatively, build in profiles or release modes to enable launching from the home screen.
このときはProfile ModeまたはRelease ModeでビルドすればiOS側で起動できるようになります。
コメント