Search code examples
iosswiftcocoapodsswift-package-managercarthage

Bundle asset retrieval broken when app is loaded from Testflight


I'm encountering an issue regarding my iOS SDK and how it retrieves and display images (from the bundle assets) when the app is downloaded from Testflight.

The SDK can be integrated via SPM, Cocoapods and Carthage. This issue is all about the Cocoapods integration.

Locally, even as a release build and when run on a device, everything runs fine and the images are being displayed:

When build locally with "Release" build configuration, the icon shows. And when the exact same app is archived and then downloaded via Testflight, all icons are gone.

This is how I retrieve the image from the bundle:

let background = UIImage(named: "select_chevrons", in: .current, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate)

and because I am distributing my SDK in 3 ways (Swift Package, cocoapods & carthage), I had to implement a BundleFinder, which defines .current:

import Foundation

private class BundleFinder {}

extension Foundation.Bundle {
    /// Returns the resource bundle associated with the current Swift module.
    var moduleName = "NAME_OF_MY_SDK"
    static var current: Bundle = {
        #if SWIFT_PACKAGE
            let bundleName = "\(moduleName)_\(moduleName)"
        #else
            let bundleName = moduleName
        #endif

        let candidates = [
            // Bundle should be present here when the package is linked into an App.
            Bundle.main.resourceURL,
            // Bundle should be present here when the package is linked into a framework.
            Bundle(for: BundleFinder.self).resourceURL,
            // For command-line tools.
            Bundle.main.bundleURL,
        ]

        for candidate in candidates {
            let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
            if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
                return bundle
            }
        }

        print("unable to find bundle named \(bundleName)")
        
        return Bundle(for: BundleFinder.self)
    }()
}

Question: Why do the images not show when the app comes from Testflight? How can I reproduce this behaviour locally?


Solution

  • I found out that this question has the exact same problem:

    Pod install has required target membership unchecked

    The problem is, that the bundle has the same name as the module. Changing the bundle name to something else fixes the issue, see my answer there: https://stackoverflow.com/a/70986055/4755172