Easy methods to make a Swift framework?


Making a Swift framework should not be onerous. This tutorial will enable you making a common framework for advanced tasks.

What’s a framework?

A framework is a hierarchical listing that encapsulates shared assets, comparable 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 to your apps.

Easy methods to make it?

There may be an article about Xcode conventions which can enable you manage your tasks, it’s best to examine that first, if you happen to haven’t earlier than.

Conventional means

There’s a conventional strategy 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 venture:

  • Create a brand new venture 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 recordsdata there
  • Create an Property folder with platforms subfolders
  • Transfer all of the Data.plist recordsdata into the right platfrom subdirectory
  • Create a Checks folder and transfer check recordsdata there

Again to Xcode:

  • Take away each group and add the brand new Folders from Finder
  • Examine that each goal has the right recordsdata (framework & checks)
  • Contained in the header file, substitute UIKit depencency with Basis

The aim is to realize a construction considerably like this:

Easy methods to make a Swift framework?

Venture settings:

  • Choose the right 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 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 information if you happen to want it
  • Write your framework you should use Swift “macros” to detect platforms

NOTE: There’s a flag in Xcode to permit app extension API solely, in case you are embedding your framework inside an software extension it ought to be enabled!

Congratulations, now you could have your model new Swift framework made within the conventional means. Let’s proceed with a neat trick.

Common cross platform framework

It’s doable to create a multiplatform single scheme Xcode venture with cross platform help for each platform, but it surely’s not beneficial as a result of it’s a hack. Nonetheless a number of open supply libraries do the identical means, 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 venture configuration file, set the xcconfig on the venture
  • Delete Data.plist recordsdata, 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 in case you are constructing for a number of platforms, nonetheless this can be a good clear strategy to help each platforms, with out duplications.

Easy methods to use a Swift framework?

Embedding your framework is probably the most simple factor to do. You’ll be able to merely drag the framework venture to a different Xcode venture, 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 final venture data tab and add the framework as a dependency.

Swift Package deal Supervisor

With SPM, it’s important to make a Package deal.swift file first, then you definately’ll be capable of construct your targets with the swift construct command. Now that Xcode helps the Swift Package deal Supervisor, it’s very easy to combine third social gathering frameworks by utilizing it.

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

Just remember to don’t miss out my deep dive into swift frameworks submit.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles