Peter-Schorn / SpotifyAPI

A Swift library for the Spotify web API. Supports all endpoints.
https://peter-schorn.github.io/SpotifyAPI/documentation/spotifywebapi
MIT License
259 stars 32 forks source link

Replace accesses to Bundle.module with .spotifyExampleContentModule. Fixes #9 #10

Closed Ivorforce closed 3 years ago

Ivorforce commented 3 years ago

Fixes #9.

sonarcloud[bot] commented 3 years ago

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Ivorforce commented 3 years ago

It's not a super pretty solution, but here's hoping the underlying error gets fixed by Apple some time in the future and you can undo this commit.

Peter-Schorn commented 3 years ago

I just tested this using the unit tests in my library and it couldn't find the bundle. Try running the "ExampleContentTests" test case (Tests/SpotifyWebAPIMainTests/Other Tests/ExampleContentTests.swift)

Peter-Schorn commented 3 years ago

Perhaps we need to use Bundle.module if all other candidates fail; e.g.:

static var spotifyExampleContentModule: Bundle = {
        /* The name of your local package, prepended by "LocalPackages_" for iOS and "PackageName_" for macOS. You may have same PackageName and TargetName*/
        let bundleNameIOS = "LocalPackages_SpotifyExampleContent"
        let bundleNameMacOs = "PackageName_SpotifyExampleContent"
        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: SpotifyExampleContentCurrentBundleFinder.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: SpotifyExampleContentCurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(),
            Bundle(for: SpotifyExampleContentCurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(),
        ]

        for candidate in candidates {
            let bundlePathiOS = candidate?.appendingPathComponent(bundleNameIOS + ".bundle")
            let bundlePathMacOS = candidate?.appendingPathComponent(bundleNameMacOs + ".bundle")
            if let bundle = bundlePathiOS.flatMap(Bundle.init(url:)) {
                return bundle
            } else if let bundle = bundlePathMacOS.flatMap(Bundle.init(url:)) {
                return bundle
            }
        }
        return Bundle.module
    }()
Peter-Schorn commented 3 years ago

Ok, I fixed the issue by changing

let bundleNameMacOs = "PackageName_SpotifyExampleContent"

to

let bundleNameMacOs = "SpotifyAPI_SpotifyExampleContent"

However, this suggests to me that we might need to change this too:

let bundleNameIOS = "LocalPackages_SpotifyExampleContent"

We must be through in testing this on every platform and in every possible condition (e.g., previews, release builds).

Ivorforce commented 3 years ago

Interesting. This might have to do with how the package is included - SpotifyAPI vs. LocalPackages suggests that the author of this code had their module bound differently. That's a bummer and makes this fix a bit more difficult...