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.