Create an Embedded Framework in Xcode with Swift

时间:2024-12-02 08:36:50

转自:http://zappdesigntemplates.com/create-an-embedded-framework-in-xcode-with-swift/

We first heard this expression at WWDC last year in 2014, Embedded Framework. A frameworkor a library is usually a bunch of classes and objects put together into one big block to be able to reuse it in different applications. The same thing is happening here. An Embedded Framework is the same thing, the only restricion it cannot contain images or other resources.

Of course you can just simply avoid using these frameworks and modify your classes’ target membership settings, and you don’t have to worry about this whole framework thing.

Create an Embedded Framework in Xcode with Swift

BUT, since we are awesome and clever developers, we want to make our code also amazing and intelligent, we should organize them into frameworks. You can even have multiple ones if you desire. One for networking and another one for the data models.

For our example, we are going to create one Embedded Framework for all of our classes that we want to access later in a Today Extension for example, or in our Watch app.

1. Add a new Target for our Embedded Framework

As every time, we need to create a new target. To do this, go to File->New->Target…

Create an Embedded Framework in Xcode with Swift

There you need to select Framework&Library and Cocoa Touch Framework.

Create an Embedded Framework in Xcode with Swift

Give it a name, we’ll name it SabWeatherDataKit and select a language and you are good to go.

Create an Embedded Framework in Xcode with Swift

Xcode will create 2 new folders/groups for you, one that contains your framework files and the test target’s files and details.

Create an Embedded Framework in Xcode with Swift

2. Setting up our framework

Now that we created this framework, we need to use it in our app somehow. In this example, we are going to move all the classes that should be or could be used in other targets such as a Today Extension. For clarity, just simply select your classes, and drag them into the SabWeatherDataKit folder. That’s it. But we are not done yet. If you select one of those classes, and open the Utilities window (right sidepanel) it will show in the Target Membership window, that it still belongs to the app not to the framework, and that’s bad.

So with the file(s) still selected, uncheck the app, and check the checkmark for your framework.

Create an Embedded Framework in Xcode with Swift

Now what Xcode does, it adds those classes to your framework and remove it from your project.

And if you build now, Xcode will cry a lot, and you’ll break your monitor trying to figure out those errors and warnings. But calm down everything is fine.

What simply happened, is that Xcode doesn’t know where those files/classes are right now, so we need to make sure to import our Framework, and all of our classes and methods are set to public (obviously the ones that we want to reach . I mean the ones, that should be visible to other classes, objects.

Wherever, you want to use any of your classes that are in the Embedded Framework now, you need to import your framework, like so:

import SabWeatherDataKit

3. Setting up your framework and project to work together as a team

Next up you need to define which classes and functions you want to make visible to other classes, targets. For example you might want to allow a login method to be called in your app, but the url constructing method that gives the proper url for the login request not.

In Swift it is simple, you just put the ‘public’ key in front of your method, class or property (in your framework’s classes) and boom, they are visible to your app.

Create an Embedded Framework in Xcode with Swift

In our example, the WeatherParser doesn’t need to be a public class, nor needs to convert its methods to public, cause it will be only used inside the framework.

4. Name your framework classes right

I know this example doesn’t fit the title of this section, but think about this for a sec. Isn’t it easier for you or someone else to find out what your class is all about by just looking at it and read the filename? So instead of LocationHandler we could use WeatherKitLocationHandler and you immediately know that this class belongs to a framework. Just as other third party frameworks are doing this: Parse, AFNetworking, etc (those are not embedded frameworks, but why can’t you do it that way?).

5. Move all the classes that can be reused into your framework

Everything that can be reused in your app, your watchapp or extension, should be moved to a framework. It is not only easier for you to manage your code, but you can make pretty great things like using a Fascade pattern to create an object that can be used to generate different types for your views.

For example in our project, we could just have a method from the framework:

downloadWeatherDataForCurrentLocation(completionBlock: (succeeded: bool, weatherArray: [Weather]) -> ())

So you don’t need to initialise a locationManager object or care about all that, you just call one method and boom, you got the weather information for your current location.

Embedded Framework is an awesome tool to group your classes, objects and create a better iOS app. Combine it with a Today extension is pretty cool. But that is for the next tutorial, coming soon.

Until that, go ahead and download the free project and play around with it. If you have any questions or concerns, use the comment section below.