Beginner question: how do I include a multi-target framework or embed its code into my project?

I'm trying to write a multiplatform (macOS + iPadOS) SwiftUI program using this project: GitHub - warrenm/GLTFKit2: A glTF 2.0 asset loader and exporter for Objective-C and Swift.

The README says:

The GLTFKit2 Xcode project is completely self-contained and can be used to build a Cocoa framework for macOS. If you want to use GLTFKit2 as a framework, link to it and embed it in your target. You can also opt to include the source directly in your app target.

Can I use the "framework" (is that like a library, or a Rust crate?) on iPadOS as well as macOS?

If not, how do I include the source in my app -- I've tried dragging either the GLTFKit2/ or the GLTFKit2/GLTFKit2/ directory onto the Xcode navigator pane, and they appear to be imported and added to the app target but import GLTFKit2 in my code gets marked "No such module".

Which is the correct directory to import, or should I copy all the files inside one of the directories next to my swift files, or should I import the GLTFKit2.xcworkspace/ or GLTFKit2/GLTFKit2.xcodeproj/ directories instead?

Hi @niconulla, welcome to the swift forums!

A framework is the Apple platform technique of packaging up libraries (and sets of libraries) as relevant precompiled binaries and supplementary information. I haven't looked into Warren's project here, but I suspect there's an Xcode project linked from within that workspace that you can use to build and create the framework. WIth the framework built, you can drag it into your own project and use it. It's not quite as straight forward as using crate from the Rust ecosystem - because in this case the framework isn't hosted in a registry with a clear path to download and easy include in your project. (For swift projects, many are getting closer to that convenience courtesy of Swift Package Manager and the online Swift Package Index).

While Apple's documentation for this process and the options available has gone rather fallow (most of what I've seen is now all archived and not being updated), there's a number of articles available - I just grabbed one from Google that might be useful to read through: Create, build and link a framework into an iOS project | by Tiago Santo | Talkdesk Engineering. The article walks you through the steps to adding a framework to an Xcode project so that the code within it can use it.

  • joe

Thanks for the welcome!

To summarize: no you can't build a framework for multiple targets, but previously you could build the framework multiple times for different targets and combine the built libraries into a "fat" or "universal" file using the command line tool lipo.

This however is the wrong way to do it now, and you should make a .xcframework as described here: xcode - Building for iOS Simulator, but the linked framework '****.framework' was built for iOS - Stack Overflow

Now I have a .xcframework which will let my program build for macOS or iPad simulator, so original question solved.

I am still curious why just including the source code into my app didn't work -- the code directory was right there, next to my Swift files, but "No such module".

If you're just trying to do gamedev for fun with Swift check out this package.

It's still pre-release, but it can do a lot and supports most swift package manager platforms.

Er, no, I'm actually trying to make a retopology tool, as I find that the most tedious part of making things in Blender. It's just a learning project for Metal though, I want to make stuff for the AR headset.

Thanks for the link to your project, I should definitely read through some real Swift code.

1 Like