Search code examples
iosswifttvos

Share a module name between two embedded frameworks for iOS and tvOS


I want to add a tvOS version to my iOS universal app. My App is written entirely in Swift and doesn't use anything fancy. Rather we stick to Cocoa Touch and try to be as platform conform as possible.

It's been amazing how seemless that works...

However I have a little problem. The iOS targets use 3 embedded frameworks. I don't seem to able to find the right settings in the project to share those embedded frameworks between the iOS and the tvOS target.

The embedded frameworks are all 3 compiled at compile time. No static libraries. Everything is in Swift.

Obviously the frameworks need to be compiled differently for tvOS than for iOS, but do I really need to create extra targets for tvOS Frameworks? Not that it would be a problem, given that both versions of the framework use the physically same source files - BUT the frameworks must have different names, as two targets (a framework is a target) can not have the same name.

That's unfortunate, because now all the shared code needs compiler directives to import the differently named frameworks. :-(

#if os(tvOS)
  import myFrameworkTV
#else
  import myFramework
#endif

Also accessing objects in those frameworks now has the same problem, if for some reason you need to use the fully qualified "path".

#if os(tvOS)
  let someVar = myFrameworkTV.aClass.aType()
#else
  let someVar = myFramework.aClass.aType()
#endif

Isn't there a way to set it up, so that I can use the same names for my Framework across both platforms?

The two cocoaPods, which I use do that?

Will it be necessary to create a second project inside the same workspace, instead of just adding targets to the existing project?

If so, what are the downsides of an embedded project vs. adding a target for tvOS to the iOS project?

One of which certainly is that the cocoaPods integration may not work as worry-free, as it does, if I just add a new target and issue a 'pod install' again.


Solution

  • Short after writing all this down, I found an easy answer to my question (as usual...):

    How to create a single shared framework between iOS and OS X

    I just need to change the PRODUCT_NAME of my additional xxx-TV frameworks, so I do not need to deal with two different names for the frameworks.