简体   繁体   中英

How to access the bundle when using Swift Package Manager

I have a framework that supports CocaPods . I have now added support for Swift Package Manager but experiencing a crash when trying to access the framework's bundle.

    static func debuggerBundle(from target: AnyClass) -> Bundle {
        let podBundle = Bundle(for:  target)
        guard let bundleURL = podBundle.url(forResource: "Harlow", withExtension: "bundle"),
        let bundle = Bundle(url: bundleURL) else { fatalError("Must have a local bundle") }
        return bundle
    }

When installing with CocoaPods , the .ipa contains a /Frameworks folder with all the frameworks. But when running with SPM , the frameworks do not exist.

Using CocoaPods

使用 CocoaPods

Using SPM

使用 SPM

How do we access the framework's bundle with SPM?

https://github.com/stanwood/Harlow/

As mentioned in the comments, SPM package version 5.3 is the minimum for supporting bundle resources. If you need to maintain support for 5.1, you can add a second package file that includes support for 5.3: Package@swift-5.3.swift .

Using your Harlow repo as a base, the new Package file will look like this:

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

let package = Package(
    name: "Harlow",
    platforms: [
        .iOS(.v10)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "Harlow",
            targets: ["Harlow"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/shu223/Pulsator.git", .upToNextMajor(from: "0.6.3")),
        .package(url: "https://github.com/schmidyy/Loaf.git", .upToNextMajor(from: "0.7.0")),
        .package(url: "https://github.com/stanwood/SourceModel_iOS.git", .upToNextMajor(from: "1.3.3"))
    ],
    targets: [
        .target(
            name: "Harlow",
            dependencies: ["Pulsator", "SourceModel", "Loaf"],
            resources: [.copy("Resources")]
        ),
        .testTarget(
            name: "HarlowTests",
            dependencies: ["Harlow"]),
    ],
    swiftLanguageVersions: [.v5]
)

This will make the Bundle.module reference become available. You could provide a static reference to this instance available through your Bundle extension like this:

extension Bundle {
    public static var harlow: Bundle = .module
}

With that in place, you should be able to use the harlow bundle reference to load your DefaultSettings.plist . ( Bundle.harlow.url(forResource:withExtension:) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM