Making a Swift framework should not be laborious. This tutorial will enable you to making a common framework for advanced initiatives.
What’s a framework?
A framework is a hierarchical listing that encapsulates shared sources, comparable to a dynamic shared library, nib information, picture information, localized strings, header information, and reference documentation in a single package deal.
So in a nutshell, a framework is a extremely reusable part to your apps.
Easy methods to make it?
There’s an article about Xcode conventions which can enable you to manage your initiatives, it’s best to verify that first, if you have not earlier than.
There’s a conventional solution to make a framework in Xcode. I’ll 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
- Comply with the directions fill & identify 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 Information.plist information into the proper platfrom subdirectory
- Create a Assessments folder and transfer take a look at information there
Again to Xcode:
- Take away each group and add the brand new Folders from Finder
- Examine that each goal has the proper information (framework & assessments)
- Contained in the header file, change UIKit depencency with Basis
The purpose is to attain a construction considerably like this:
- Choose the proper plist information for the targets
- Set your bundle identifiers (use my conventions)
- Setup platform variations (recommendation: help 1 older model too)
- Setup the plist information for the assessments from the construct settings pane
- Set the product identify (Console) in your framework construct settings
- Examine your construct phases and add the general public header file.
- Go to the scheme settings and setup shared schemes for the frameworks
- Collect protection knowledge in the event you want it
- Write your framework you need to use Swift “macros” to detect platforms
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’ve your model new Swift framework made within the conventional approach. Let’s proceed with a neat trick.
Common cross platform framework
It’s attainable to create a multiplatform single scheme Xcode mission with cross platform help for each platform, however it’s not really helpful as a result of it is a hack. Nonetheless a number of open supply libraries do the identical approach, so why should not we.
- Delete all of the targets, schemes, besides macOS!!!
- Rename the remaining goal, scheme (we do not want platform names)
- Use the mission configuration file, set the xcconfig on the mission
- Delete Information.plist information, use one for the framework and one for the assessments
- Rename bundle identifier (we do not want platform names there too)
States will be blended up if you’re constructing for a number of platforms, nevertheless it is a good clear solution to help each platforms, with out duplications.
Easy methods to use a Swift framework?
Embedding your framework is probably the most easy factor to do. You may merely drag the framework mission to a different Xcode mission, the one factor left to do is to the embedded the framework into the appliance. You may go to the embedded binaries part inside the final mission data tab and add the framework as a dependency.
Swift Bundle Supervisor
With SPM, you need to make a
Bundle.swift file first, then you can construct your targets with the
swift construct command. Now that Xcode helps the Swift Bundle Supervisor, it is very easy to combine third get together frameworks through the use of it.
You may obtain the ultimate framework examples from GitHub.
Just remember to do not miss out my deep dive into swift frameworks put up.
Leave a Reply