image_pickerを使うとアプリがクラッシュする!

に公開

Tips💡

Flutter3.19.0にアプリをアップデートしたせいなのか、画像のアップロードをしようとしたら、アプリがクラッシュした!!!

「なぜだ😱」

やったこと

  1. packageの更新
  2. 公式のリファレンスをチェック
  3. info.plistをチェック

どうやらアプリを作り直した時に、xmlを追加していなかったようだ😅
[これを追加する]

<key>NSCameraUsageDescription</key>
	<string>Access to take a photo by camera</string>
	<key>NSAppleMusicUsageDescription</key>
	<string>Access to pick a photo</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>Access to pick a photo</string>
	<key>CADisableMinimumFrameDurationOnPhone</key>
	<true/>

[こんな感じ]

ios/Runner/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <!-- image_picker xml start -->
	<key>NSCameraUsageDescription</key>
	<string>Access to take a photo by camera</string>
	<key>NSAppleMusicUsageDescription</key>
	<string>Access to pick a photo</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>Access to pick a photo</string>
	<key>CADisableMinimumFrameDurationOnPhone</key>
	<true/>
	<!-- image_picker xml end -->
	<key>CFBundleDevelopmentRegion</key>
	<string>$(DEVELOPMENT_LANGUAGE)</string>
	<key>CFBundleDisplayName</key>
	<string>Workstream Prod</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>workstream_prod</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleShortVersionString</key>
	<string>$(FLUTTER_BUILD_NAME)</string>
	<key>CFBundleSignature</key>
	<string>????</string>
	<key>CFBundleURLTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>CFBundleURLSchemes</key>
			<array>
				<string>io.supabase.flutterquickstart</string>
			</array>
		</dict>
	</array>
	<key>CFBundleVersion</key>
	<string>$(FLUTTER_BUILD_NUMBER)</string>
	<key>LSRequiresIPhoneOS</key>
	<true/>
	<key>UIApplicationSupportsIndirectInputEvents</key>
	<true/>
	<key>UIBackgroundModes</key>
	<array>
		<string>fetch</string>
		<string>remote-notification</string>
	</array>
	<key>UILaunchStoryboardName</key>
	<string>LaunchScreen</string>
	<key>UIMainStoryboardFile</key>
	<string>Main</string>
	<key>UISupportedInterfaceOrientations</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
	<key>UISupportedInterfaceOrientations~ipad</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationPortraitUpsideDown</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
</dict>
</plist>

最後に

公式のGithubの設定を見ると同じ設定のようですね。写真とカメラへのアクセスが許可されていないからクラッシュしたということですね。

https://github.com/flutter/packages/blob/main/packages/image_picker/image_picker/example/ios/Runner/Info.plist

参考になった過去の記事。
https://zenn.dev/seya/articles/b5b9d82b68f198

Discussion