I have a Swift Package, it's added to both the main app and the autofill extension. The main app is an iOS app that run directly on my mac from Xcode.
I use this extension to access the bundle
import Foundation
import OSLog
class CurrentBundleFinder {}
extension Foundation.Bundle {
static let myModule: Bundle = { /* The name of your local package, prepended by "LocalPackages_" */
let bundleName = "DesignSystem_DesignSystem"
let logger = Logger(subsystem: "DesignSystem", category: "Bundle")
logger.error("Searching for bundle: \(bundleName)")
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: CurrentBundleFinder.self).resourceURL, /* For command-line tools. */
Bundle.main.bundleURL, /* Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). */
Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(), /* For app extensions - look in parent app bundle */
Bundle.main.bundleURL.deletingLastPathComponent().deletingLastPathComponent(),
]
logger.error("all bundle: \(candidates, privacy: .public)")
for (index, candidate) in candidates.enumerated() {
logger.error("Checking candidate \(index): \(candidate?.absoluteString ?? "nil", privacy: .public)")
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
logger.error("Bundle path: \(bundlePath?.absoluteString ?? "nil", privacy: .public)")
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
logger.error("Successfully found bundle at: \(bundlePath?.absoluteString ?? "unknown", privacy: .public)")
return bundle
}
}
logger.error("Unable to find bundle named \(bundleName)")
fatalError("unable to find bundle named \(bundleName)")
}()
}
Bellow is the log from the main app and the autofill extension
I have check that /private/var/folders/cb/fctmx0_x3_dbxy_9wnm7g0s40000gn/X/1CC84EBB-DAC0-5120-9346-5EFBC8691CF1/d/Wrapper/Proton Pass.app/PlugIns/AutoFill.appex/DesignSystem_DesignSystem.bundle
exist in the file system, but the autofill extension is unable to create a bundle from that
That’s some pretty complicated code you have there. It sounds like you want the same code to run in both your app and your appex. If so, the canonical way to do that is to put the code, and its resources, into a framework. Your framework can then access its resources as it usually would.
A key advantage of this approach is that your app only ships with a single copy of the framework’s code.
So, I recommend that you aim for a structure like this:
MyApp.app/
Info.plist
MyApp
PlugIns/
MyExtension.appex/
Info.plist
MyExtension
Frameworks/
MyFramework.framework/
Info.plist
MyFramework
MyBundle.bundle/
Info.plist
…
Within your framework, you can either use the +bundleForClass:
trick (à la CurrentBundleFinder
) or use Swift Package Manager’s Bundle.module
support.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"