Create an Embedded Framework in Xcode with Swift

Source: Internet
Author: User

Transferred from: http://zappdesigntemplates.com/create-an-embedded-framework-in-xcode-with-swift/

Post series:create a Weather app with Embedded Framework & Today Extension
    • 1.Create an Embedded Framework in Xcode with Swift
    • 2.How to make a Today Extension in Swift for a Weather app

We first heard this expression at WWDC, the Embedded Framework. A Frameworkor a library is usually a bunch of the classes and objects put together into one big block to being able to reuse it I n different applications. The same thing is happening here. An Embedded Framework is the same thing, and 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 N ' t has to worry on this whole framework thing.

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

For our example, we is going to the create one Embedded Framework for all of our classes so we want to access later in A to Day Extension to 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 does this, go to file->new->target ...

There need to select Framework&library and Cocoa Touch Framework.

Give it a name, we'll name it Sabweatherdatakit and select a language and you're good to go.

Xcode would create 2 new folders/groups for you, one that contains your framework files and the test target's files and Det Ails.

2. Setting up our framework

Now the We created this framework, we need to use it in our app somehow. In this example, we is going to move all the classes that should is or could be used in other targets such as a Today Ext Ension. For clarity, just simply select your classes, and drag them into the Sabweatherdatakit folder. That ' s it. But we were not doing yet. If you select one of those classes, and open the Utilities window (right sidepanel) it'll show in the Target membership window, that's it still belongs to the app is 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.

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

And if you build now, Xcode would cry a lot, and you'll break your monitor trying into figure out those errors and warnings. But calm-everything is fine.

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

Wherever, want to use any of your classes that is in the Embedded framework now, you need to import your framework, L IKE 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 your might want to allow a login method to is called in your app, and the URL constructing method that gives th E proper URL for the login request is not.

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

In we example, the weatherparser doesn ' t need to being a public class, or needs to convert their methods to public, cause it 'll is 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 some One else to find out what your class was all about by just looking at it and read the filename? So instead of Locationhandler we could use Weatherkitlocationhandler and you immediately know the This class belongs to a Framework. Just as other third party frameworks is doing this:parse, afnetworking, etc (those is not embedded frameworks, and why Can ' t do it?).

5. Move all the classes, can be reused to your framework

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

For example with our project, we could just has a method from the framework:

Downloadweatherdataforcurrentlocation (Completionblock: (Succeeded:bool, Weatherarray: [Weather])

So your don ' t need to initialise a Locationmanager object or care about all that, you just call one method and boom, you go T the weather information for your.

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's for the next tutorial, coming soon.

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

Create an Embedded Framework in Xcode with Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.