Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CRASH] SwiftTweaks crashes when integrated via Cocoapods with the new Resource bundles change #161

Open
1 task done
JoeSzymanski opened this issue Oct 28, 2022 · 2 comments

Comments

@JoeSzymanski
Copy link

JoeSzymanski commented Oct 28, 2022

  • I have read through CONTRIBUTING.md

Describe the bug
When integrating via Cocoapods (using the new resource bundles), SwiftTweaks crashes when navigating to a sub-menu.

To Reproduce
Steps to reproduce the behavior:

  1. Integrate SwiftTweaks through Cocoapods
  2. Run the app and open the SwiftTweaks menu
  3. Navigate to a submenu
  4. Notice the crash

Expected behavior
The submenu is displayed.

Additional context
The code that's causing the issue in question is the following:

	// NOTE (bryan): if we just used UIImage(named:_), we get crashes when running in other apps!
	// (Why? Because by default, iOS searches in your app's bundle, but we need to redirect that to the bundle associated with SwiftTweaks
	private convenience init?(inThisBundleNamed imageName: String) {
		#if SWIFT_PACKAGE
		self.init(named: imageName, in: Bundle.module, compatibleWith: nil)
		#else
		self.init(named: imageName, in: Bundle(for: TweakTableCell.self), compatibleWith: nil) // NOTE (bryan): Could've used any class in SwiftTweaks here.
		#endif
	}

Note that because the resource bundle now is a named bundle and not directly part of the SwiftTweaks framework itself, Bundle(for: TweakTableCell.self) will no longer work. This should be updated to reference the new resource bundle directly, though this might also impact other integrations.

@JoeSzymanski JoeSzymanski changed the title [BUG] [CRASH] SwiftTweaks crashes when integrated via Cocoapods with the new Resource bundles change Oct 28, 2022
@bryanjclark
Copy link
Owner

Nice catch, thanks!

@JoeSzymanski
Copy link
Author

For reference, we use a solution like the code below to handle this case in a way that supports both static and dynamic integration through Cocoapods:

        let bundleName = "SwiftTweaks"
        if let bundleURL = Bundle(for: TweakTableCell.self).url(forResource: bundleName, withExtension: "bundle"), let bundle = Bundle(url: bundleURL) {
            return bundle
        } else if let bundleURL = Bundle.main.url(forResource: bundleName, withExtension: "bundle"), let bundle = Bundle(url: bundleURL) {
            return bundle
        } else {
            return nil
        }

I'm not sure how this would interact with other forms of integration such as SPM or Carthage, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants