Skip to main content

info-plist

在 Apple 生態(iOS、iPadOS、macOS)中,Info.plist屬性列表檔 (Property List),作為 應用設定與中繼資料:描述 App 的識別、啟動方式、權限需求與平台整合。系統在載入 App 時會先讀取 Info.plist,並依設定決定執行行為。

常見用途

  1. App 基本資訊

    • CFBundleName:App 名稱
    • CFBundleIdentifier:App 的唯一識別 ID(像 com.example.myapp
    • CFBundleVersion / CFBundleShortVersionString:版本號
  2. App 行為設定

    • UILaunchStoryboardName:指定啟動畫面 (LaunchScreen)
    • UIInterfaceOrientation:支援的螢幕方向
    • UIRequiresFullScreen:是否強制全螢幕
  3. 權限宣告(很重要)

    • iOS 需要在 Info.plist 宣告才能呼叫敏感 API,否則會直接 crash

    • 例如:

      • NSCameraUsageDescription:使用相機的理由
      • NSBluetoothAlwaysUsageDescription:使用藍牙的理由
      • NSLocationWhenInUseUsageDescription:使用定位的理由
  4. 平台整合

    • URL Schemes (CFBundleURLTypes):允許用特定 URL 打開你的 App
    • App Transport Security (ATS) 設定:允許 HTTP 或自訂網路連線規則
    • Push Notifications / Background Modes:支援背景下載、藍牙、通知等

📌 簡單來說,Info.plist 就像 App 的身分證 + 使用說明書,讓系統知道:

  • 這個 App 是誰
  • 它要怎麼跑
  • 它需要什麼資源/權限

以下為常用的 Info.plist 範例,涵蓋 一般 App 必備資訊 + 常見權限設定。適用於 原生 iOS AppCapacitor/Cordova 專案,可視需求調整。

📄 範例 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>

<!-- 基本資訊 -->
<key>CFBundleName</key>
<string>MyApp</string>
<key>CFBundleDisplayName</key>
<string>MyApp</string>
<key>CFBundleIdentifier</key>
<string>com.example.myapp</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1</string>

<!-- 啟動畫面 & 主 Storyboard -->
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>

<!-- 支援的螢幕方向 -->
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>

<!-- 背景模式 (例如藍牙 / push / background fetch) -->
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
<string>fetch</string>
<string>remote-notification</string>
</array>

<!-- App Transport Security (允許 HTTP,開發測試時常用) -->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

<!-- 常見權限宣告 (要用到相關 API 就要加,不然會 crash) -->
<key>NSCameraUsageDescription</key>
<string>此 App 需要相機來拍照或掃描 QR Code</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>此 App 需要藍牙來連接裝置</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>此 App 需要藍牙來與周邊裝置互動</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>此 App 需要定位服務來提供位置相關功能</string>
<key>NSMicrophoneUsageDescription</key>
<string>此 App 需要麥克風來錄音或語音控制</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>此 App 需要相簿存取權限來選擇照片</string>
<key>NSContactsUsageDescription</key>
<string>此 App 需要聯絡人權限來邀請朋友</string>

<!-- URL Schemes (允許外部用 myapp:// 開啟 App) -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.myapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>

</dict>
</plist>

✅ 重點說明

  1. 基本資訊 (CFBundleIdentifier, CFBundleVersion) → App 的身份 & 版本號。
  2. UI/啟動畫面 (UILaunchStoryboardName) → 讓系統知道要顯示哪個啟動畫面。
  3. 背景模式 (UIBackgroundModes) → 藍牙、推播、背景下載需要加。
  4. 權限宣告 (以 NS...UsageDescription 開頭) → 必加,不然一呼叫 API 就直接 crash。
  5. ATS 設定 (NSAppTransportSecurity) → iOS 預設不允許 HTTP,開發時常先打開。
  6. URL Schemes → 讓別的 App 或瀏覽器可以用 myapp:// 來喚起你的 App。