What’s a framework?
A framework is a hierarchical listing that encapsulates shared sources, equivalent to a dynamic shared library, nib recordsdata, picture recordsdata, localized strings, header recordsdata, and reference documentation in a single package deal.
So in a nutshell, a framework is a extremely reusable part on your apps.
Easy methods to make it?
There may be an article about Xcode conventions which is able to enable you to manage your tasks, you must test that first, if you have not earlier than.
Conventional manner
There’s a conventional solution to make a framework in Xcode. I will 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 undertaking:
- Create a brand new undertaking 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 line with the platform names
Now in Finder:
- Create a Sources folder and transfer all of the Swift and header recordsdata there
- Create an Property folder with platforms subfolders
- Transfer all of the Information.plist recordsdata into the proper platfrom subdirectory
- Create a Exams folder and transfer take a look at recordsdata there
Again to Xcode:
- Take away each group and add the brand new Folders from Finder
- Examine that each goal has the proper recordsdata (framework & assessments)
- Contained in the header file, substitute UIKit depencency with Basis
The aim is to attain a construction considerably like this:
Venture settings:
- Choose the proper plist recordsdata for the targets
- Set your bundle identifiers (use my conventions)
- Setup platform variations (recommendation: help 1 older model too)
- Setup the plist recordsdata 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.
Scheme settings:
- 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 should utilize 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 software extension it must be enabled!
Congratulations, now you’ve got your model new Swift framework made within the conventional manner. Let’s proceed with a neat trick.
Common cross platform framework
It’s potential to create a multiplatform single scheme Xcode undertaking with cross platform help for each platform, nevertheless it’s not advisable as a result of it is a hack. Nonetheless a number of open supply libraries do the identical manner, 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 undertaking configuration file, set the xcconfig on the undertaking
- Delete Information.plist recordsdata, use one for the framework and one for the assessments
- Rename bundle identifier (we do not want platform names there too)
States may be combined up if you’re constructing for a number of platforms, nevertheless this can be 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 possibly can merely drag the framework undertaking to a different Xcode undertaking, the one factor left to do is to the embedded the framework into the appliance. You possibly can go to the embedded binaries part inside the overall undertaking information tab and add the framework as a dependency.
Swift Bundle Supervisor
With SPM, you must make a Bundle.swift file first, then you’ll construct your targets with the swift construct command. Now that Xcode helps the Swift Bundle Supervisor, it is very easy to combine third celebration frameworks through the use of it.
You possibly can obtain the ultimate framework examples from GitHub.
Just be sure you do not miss out my deep dive into swift frameworks submit.