Making a Swift framework should not be arduous. This tutorial will allow you to making a common framework for complicated initiatives.
What’s a framework?
A framework is a hierarchical listing that encapsulates shared assets, comparable to a dynamic shared library, nib information, picture information, localized strings, header information, and reference documentation in a single bundle.
So in a nutshell, a framework is a extremely reusable element to your apps.
How you can make it?
There’s an article about Xcode conventions which can allow you to manage your initiatives, you need to test that first, if you happen to haven’t earlier than.
Conventional method
There’s a conventional method to make a framework in Xcode. I’m going to create a shared framework for all of the apple platforms (iOS, macOS, tvOS, watchOS), which goes to be able to logging issues to the usual console.
Let’s make the mission:
- Create a brand new mission utilizing one of many framework targets
- Observe the directions fill & title all of the fields
- Add all the opposite platform framework targets
- Rename all targets in keeping with the platform names
Now in Finder:
- Create a Sources folder and transfer all of the Swift and header information there
- Create an Belongings folder with platforms subfolders
- Transfer all of the Data.plist information into the right platfrom subdirectory
- Create a Checks folder and transfer check information there
Again to Xcode:
- Take away each group and add the brand new Folders from Finder
- Examine that each goal has the right information (framework & checks)
- Contained in the header file, exchange UIKit depencency with Basis
The purpose is to realize a construction considerably like this:
Challenge settings:
- Choose the right plist information for the targets
- Set your bundle identifiers (use my conventions)
- Setup platform variations (recommendation: assist 1 older model too)
- Setup the plist information for the checks from the construct settings pane
- Set the product title (Console) in your framework construct settings
- Examine your construct phases and add the general public header file.
Scheme settings:
- Go to the scheme settings and setup shared schemes for the frameworks
- Collect protection knowledge if you happen to want it
- Write your framework you need to use Swift “macros” to detect platforms
NOTE: There’s a flag in Xcode to permit app extension API solely, if you’re embedding your framework inside an utility extension it needs to be enabled!
Congratulations, now you could have your model new Swift framework made within the conventional method. Let’s proceed with a neat trick.
Common cross platform framework
It’s doable to create a multiplatform single scheme Xcode mission with cross platform assist for each platform, but it surely’s not advisable as a result of it’s a hack. Nonetheless a number of open supply libraries do the identical method, so why shouldn’t we.
- Delete all of the targets, schemes, besides macOS!!!
- Rename the remaining goal, scheme (we don’t want platform names)
- Use the mission configuration file, set the xcconfig on the mission
- Delete Data.plist information, use one for the framework and one for the checks
- Rename bundle identifier (we don’t want platform names there too)
WARN: States might be blended up if you’re constructing for a number of platforms, nonetheless this can be a good clear method to assist each platforms, with out duplications.
How you can use a Swift framework?
Embedding your framework is essentially the most easy factor to do. You possibly can merely drag the framework mission to a different Xcode mission, the one factor left to do is to the embedded the framework into the applying. You possibly can go to the embedded binaries part inside the final mission data tab and add the framework as a dependency.
Swift Package deal Supervisor
With SPM, it’s a must to make a Package deal.swift file first, you then’ll have the ability to construct your targets with the swift construct command. Now that Xcode helps the Swift Package deal Supervisor, it’s very easy to combine third get together frameworks through the use of it.
You possibly can obtain the ultimate framework examples from GitHub.
Just be sure you don’t miss out my deep dive into swift frameworks publish.