HomeiOS DevelopmentLearn how to make a Swift framework?

Learn how to make a Swift framework?


Making a Swift framework should not be exhausting. This tutorial will show you how to making a common framework for complicated tasks.

Swift

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 element in your apps.


Learn how to make it?

There may be an article about Xcode conventions which is able to show you how to set up your tasks, you need to verify that first, if you have not earlier than.


Conventional means

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 challenge:

  • Create a brand new challenge 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 response to the platform names

Now in Finder:

  • Create a Sources folder and transfer all of the Swift and header recordsdata there
  • Create an Belongings folder with platforms subfolders
  • Transfer all of the Information.plist recordsdata into the proper platfrom subdirectory
  • Create a Checks 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 & exams)
  • Contained in the header file, exchange UIKit depencency with Basis

The objective is to realize 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 exams 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 for those who 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, in case you are 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 means. Let’s proceed with a neat trick.



Common cross platform framework

It’s attainable to create a multiplatform single scheme Xcode challenge with cross platform help for each platform, nevertheless it’s not really helpful as a result of it is a hack. Nevertheless a number of open supply libraries do the identical means, 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 challenge configuration file, set the xcconfig on the challenge
  • Delete Information.plist recordsdata, use one for the framework and one for the exams
  • Rename bundle identifier (we do not want platform names there too)

States could be combined up in case you are constructing for a number of platforms, nonetheless this can be a good clear solution to help each platforms, with out duplications.



Learn how to use a Swift framework?

Embedding your framework is essentially the most easy factor to do. You’ll be able to merely drag the framework challenge to a different Xcode challenge, the one factor left to do is to the embedded the framework into the appliance. You’ll be able to go to the embedded binaries part inside the overall challenge data tab and add the framework as a dependency.


Swift Package deal Supervisor

With SPM, you need to make a Package deal.swift file first, then you can construct your targets with the swift construct command. Now that Xcode helps the Swift Package deal Supervisor, it is very easy to combine third occasion frameworks by utilizing it.

You’ll be able to obtain the ultimate framework examples from GitHub.

Just remember to do not miss out my deep dive into swift frameworks put up.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments