Creating a Framework for both iOS and watchOS

The try! Swift app runs on both iOS and watchOS, and I’d like to expand it to support fancy extensions and possibly something with iMessage. And maybe I’ll want to have a tvOS app eventually to hook up to the tv monitors around one of my conferences.

While I was ok duplicating the Model layer between the two platforms initially, as more extensions and platforms are something I want to code quickly on, it was time to move that code out into a framework.

I haven’t made a framework before, but it’s super easy. The hard part was realizing that there there isn’t an option to create a cross-platform framework. I was forced to choose between iOS, macOS, tvOS, and watchOS. Which makes a lot of sense. Apple frameworks that are available on iOS are not available on watchOS, etc.

But in my case, I just had super simple Model-layer code, so I wanted to share this code between my iOS and watchOS platforms. After searching around for how to do it, I finally found a simple solution that works.

I just created a Models folder for shared files, and put it in a folder outside the two frameworks:

shared framework code

Then I just dragged the files into to the correct framework targets (do NOT select “Copy as needed”).

Model Duplication

The key here is that if you want to add something to your model layer that only pertains to the specific framework, you have to make a new file with an extension for that model. Oh, and of course, the files have the exact same reference, so any change you make to one framework, you make to the other.

So it’s definitely a bit dangerous and not a great solution, but the only one I’ve found unfortunately. Now I finally feel like I can move fast with other app extensions I have in mind!

If anyone has better solutions, happy to hear about them in the comments!

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • Михайло Вишневський

    Hi, do u hear about BNNS? Or something about machine learning on swift?

  • Ilya Puchka

    There is a way to create multi-platform frameworks with just one target. http://ilya.puchka.me/xcode-cross-platform-frameworks/

  • Pedro Piñera

    I was about to say the same. You can use this .xcconfig file with your framework and then you make it multi platform. You have to pay attention to the APIs of Apple’s framework because they may differ across platforms.
    https://github.com/pepibumur/framework-xcconfig/blob/392a70367fa86572f34cd8e166d67202c0d5c439/framework.xcconfig

  • Tof

    Hi, Have one source for multiple platforms is not complicated. Take a look of this article
    “Creating Cross-Platform Swift Frameworks for iOS, watchOS, and tvOS via Carthage and CocoaPods” (https://goo.gl/aLWCli)

  • Bruno Fernandes

    Why not add as workspace, and not a project in another project? I believe that it’s a better practice to organize the project!

  • azam3d

    Can I use it for Realm models?

  • Chirag Ramani

    Hey..I have added my files and targets are some framworks… Files have a public class. But the description of the class instances are different in different frameworks so my downcast does not work at all…Any suggestions how to handle this?