You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bundle.main.pathForResource返回nil:为何找不到指定目录的GoogleService-Info.plist?

Why Your GoogleService-Info.plist Path Lookup Fails

Hey there, let’s break down exactly why your code is returning nil and how to fix it:

1. Xcode Doesn’t Preserve Folder Groups in the Bundle (By Default)

When you add folders to your project using the default yellow "Groups" option, Xcode doesn’t copy the original directory structure into your app’s bundle. That means your GoogleService-Info.plist files from Resources/Staging and Resources/Production are both dumped directly into the root of your app bundle—not inside nested Resources/Staging or Resources/Production subfolders.

So when you specify inDirectory: "Resources/Staging", the bundle doesn’t even have that subfolder to begin with, hence the nil result.

2. Duplicate Filenames Cause Overwriting

Making things worse: since both files share the exact same name, only one of them will actually survive the build process. Xcode will overwrite the first plist with the second during the copy phase—so even if you tried to look for it at the bundle root, you’d only get one version, not both.

Fixes to Try

Option 1: Use Folder References to Preserve Directory Structure

  • First, remove the existing Resources folder from your project (choose "Remove References Only" to keep your actual files intact).
  • Re-drag the Resources folder into Xcode, and when the import dialog pops up, select "Create folder references" (this will make the folder appear blue in your project navigator).
  • Now Xcode will copy the full directory structure into the bundle. Your Staging and Production subfolders will exist inside the bundle’s Resources folder, and both plists will stay separate.
  • Update your code to match the preserved structure:
    if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist", inDirectory: "Resources/Staging") {
        print("Found the staging plist!")
    }
    

Option 2: Rename Plists to Avoid Conflicts

A simpler workaround is to give the files unique names:

  • Rename the staging file to GoogleService-Info-Staging.plist and the production one to GoogleService-Info-Production.plist.
  • You can keep using yellow groups since the filenames no longer clash.
  • Access them directly without the inDirectory parameter:
    // For staging
    if let path = Bundle.main.path(forResource: "GoogleService-Info-Staging", ofType: "plist") {
        print("Found staging plist!")
    }
    
    // For production
    if let path = Bundle.main.path(forResource: "GoogleService-Info-Production", ofType: "plist") {
        print("Found production plist!")
    }
    

Verify the Bundle Contents

To double-check what’s actually in your app bundle:

  1. Build your project.
  2. In the Project Navigator, navigate to the Products folder.
  3. Right-click your app’s .app file and select "Show Package Contents".
  4. Check if the directory structure or plist files match what you expect—this will help you debug any lingering issues.

内容的提问来源于stack exchange,提问作者Tometoyou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:37:29