Most of the content of this article is translated from a tutorial by Raywenderlich, but it is not a translation. There is no complete format and order of the original text, but some order adjustment and deletion, and some new and complete content on the basis of the original text

Environment:

  • Xcode Version 9.3 (9 e145),
  • Cocoapods 1.1 +,
  • iOS 8+
  • Swift/Objective-C

1 the introduction

1.1 according to

Why do we need an open source library or Framework?

  • Code encapsulation
  • Code modularization
  • Code reuse

Modern software development, of course, is more about teamwork, and while there are good indie developers out there, they rely on open source libraries to do it all by themselves. So sharing code is a must.

1.2 How

We have two common approaches:

  • Framework
  • Open source library

Framework is more closed, it hides the implementation of the source code, often used in commercial software, for example, you provide some service to the customer, but do not want the customer to know the implementation details, to protect trade secrets, Framework is the best choice

The open source library, as the name implies, is completely open source. GitHub, as we all know, is the free code repository of the open source library. It is more open and opens source code according to MIT and other agreements

Raywenderlich’s tutorial focuses on how to create a Framework, but the open source library works the same way, so I added some content from the open source library

Android 1.3

In fact, I have written a similar article, but on Android to create the Framework, and use Maven to distribute, this is the corresponding iOS platform, and use Cocoapod to distribute, please like

2 make Framework

If you search for technical articles about making the iOS Framework, you’re likely to find a lot of early ones that are daunting, but after Xcode 6 and iOS 8, Cocoa Touch Framework, Cocoa Touch Framework, Cocoa Touch Framework, Cocoa Touch Framework, Cocoa Touch Framework

First please download xcode project Phonercise in the original tutorial. This project is a small function written by Swift. Our goal is to make this function into a Framework that can be used in other projects

2.1 Create a Cocoa Touch Framework project

  1. In Xcode, select New Project (File/New/Project.)
  2. Select Cocoa Touch Framework in the new type
  3. Select Next
  4. Set the project name to ThreeRingControl and select the unit test option
  5. Put this project in the same file path as the Phonercise project you downloaded earlier for later operations
  6. Create a project

2.2 Adding Modules and Resource Files

Now your Framework project has been created, but of course it is empty, so you need to drag the source file from the Phonercise project, as shown below

  • CircularGradient.swift
  • coin07.mp3
  • Fanfare.swift
  • RingLayer.swift
  • RingTip.swift
  • ThreeRingView.swift
  • Utilities.swift
  • winning.mp3

Remember to select the Copy items if needed option when dragging and dropping, if not, it is a reference mode, your changes in this project will affect the original project, if selected, it is Copy, that is, the source file Copy to this project, the two projects do not affect each other.

For copy mode, you should see the current project name ThreeRingControl in the file’s Target Membership selection, as shown below

2.3 add Framework

2.3.1 Test engineering

Then you can build the project to make sure there are no errors. Of course, you can only build, you can’t run, you can’t run on a real machine or emulator, because remember? This is the CocoaTouch Framework project, not the usual application project. So you can’t run, see the picture below (engineering picture is a small yellow briefcase)

So the question is, how can I test it? I can’t just send a Framework file every time, integrate it into a project, and test it once? It’s so low.

The easiest thing to do, of course, is to write code and run it as usual. So we also need a test project.

Phonercise project is the best test project, the original code is copied from Phonercise project, even how to call the code is already ready, we in Step 2.2 copy 8 files into the ThreeRingControl project, Now as long as the eight of the Phonercise file deletion, then put ThreeRingControl. The Framework is integrated into, if the program is not an error, effect is the same, it shows that our Framework has been completed.

Ok, go delete these 2 files ~

2.3.2 add Framework

After deleting 8 files, close the ThreeRingControl project first because project files cannot be opened more than once.

In Phonercise choose add file, and then ThreeRingControl ThreeRingControl under the project file path. Xcodeproj file added, pay attention to, be sure to turn off ThreeRingControl project.

This makes ThreeRingControl a sub-project for Phonercise. You can see all of ThreeRingControl on Phonercise for future debugging.

Note: I have a bug that sometimes when I drag it in, I don’t see the file under ThreeRingControl. It’s like dragging a file in instead of a project, so close Xcode and open it again.

Of course, the project will still not run, a lot of errors, you also need to integrate the Framework, as shown below.

2.3.3 Access Control

If you click build in a hurry, you’ll still get a bunch of errors because the files were in the same module, but now they’re in a different module and need to be referenced

Open the ActionViewController. Swift file, add the following code

import ThreeRingControl
Copy the code

In addition, Swift has a security feature called Access Control, which limits the level of access that code in other source files or modules can have to your code. This feature allows us to hide some implementation details of our code and provides interfaces to code that others can access and use.

  • The Open and Public levels allow entities to be accessed by all entities in the same module source file, and outside the module, all entities in the source file can be accessed by importing the module. Typically, you would use the Open or Public level to specify the framework’s external interface.
  • The Internal level allows entities to be accessed by any entity in the same module source file, but not by entities outside the module. Typically, if an interface is only used within an application or framework, it can be set to the Internal level.
  • File-private restricts access to entities only within the files they define. Use file-private to hide functionality details if they only need to be used within a File.
  • Private restricts entities to be accessible only in their defined scope, as well as extension within the same file. You can use Private to hide some of the details of a function if they only need to be used in the current scope.

So, we need to make something public to allow someone to call, open the threeringView.swift file, and add the public keyword

public class ThreeRingView : UIView {
Copy the code

In addition, you need to add the public keyword in the following content

  • The init method
  • RingCompletedNotification, AllRingsCompletedNotification, layoutSubviews()
  • Attributes of the @ibInspectable tag, nine in total

Public: ringSound, allRingSound, sharedInstance, playSoundsWhenReady()

2.3.4 call Framework

In the Phonercise project, ThreeRingControl is called in the storyboard, so we still have some work to do

  1. Open the Phonercise project main.storyboard
  2. Choose Ring Control
  3. Switch to Identity Inspector and select ThreeRingControl in the Module under the Custom Class

At this point, you can finally run successfully. Give it a try.

3 create cocoapod

CocoaPod is one of the most popular iOS dependency management tools available today. A POD is an open source library that is similar to a Framework and includes a code and resource file. By putting the Framework or source code into a POD, you have the ability to distribute your code.

3.1 Preparations

  1. In Phonercise project selected “ThreeRingControl. Xcodeproj”, and Remove him, delete, select “Remove the Reference”.
  2. If you don’t have Cocoapod, please install the CocoaPods Installation Guide

3.2 create a pod

  1. Open a Terminator and enter the directory where ThreeRingControl is located
  2. Execute command:pod spec create ThreeRingControl“, and a file named ThreeringControl. podspec will be created. This podspec file can be found in any open source library such as AFNetworking or YYModel on Github
  3. Open it with text editor or Vim and edit it
  4. Summary, description, and homepage must be changed to the default values. Otherwise, other users will not be able to access the homepagepod install
s.name         = "ThreeRingControl"
s.version      = "1.0.0"
s.summary      = "A three-ring control like the Activity status bars"
s.description  = "The three-ring is a completely customizable widget that can be used in any iOS app. It also plays a little victory fanfare."
s.homepage     = "http://raywenderlich.com"
Copy the code
  1. Select a License, for example, S. License = “MIT”.

  2. Specify the lowest platform version as required: s.platform = :ios, “10.0”

  3. This field tells the installer where the source file is and where to get the source file. This field is usually github’s address

    For example, in AFNetworking’s PodSpec, this field looks like this

    s.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => s.version, :submodules => true }

    But we haven’t built Git yet, so for now we’ll write it in the current directory, where the podspec file is located.

  4. The source_files field specifies which source files are to be published. Not all files in a project need to be published. Some demo and Test files do not need to be published, so this field will be published.

    s.source_files = "ThreeRingControl", "ThreeRingControl/**/*.{h,m,swift}"

  5. Resources = xib, nib, Image, etc.

    s.resources = "ThreeRingControl/*.mp3"

  6. Specify swift version, which is required because there are multiple versions of swift and each version has some incompatible writing

    s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3' }

3.3 Adding a Dependency

3.3.1 Third-party POD Library Dependencies

However, we have seen some libraries that rely on third parties, such as YTKNetwork, which actually relies on AFNetworking, and we can see that it has a field in its PodSpec

Spyware doctor ependency AFNetworking ", "" - > 3.0"

This means that when you install YTKNetwork, Pod will automatically check if you have AFNetworking at version 3.0. If you have AFNetworking, pod will continue and install it for you if you don’t. This is a big part of cocoapod’s popularity, dependency management

Framework is dependent on

If you rely on projects that are managed and published by Cocoapod, there are dependencies that are not managed by POD, such as another Framework, SDWebImage, which relies on ImageIO, one of Apple’s own frameworks, The following fields are used:

s.framework = 'ImageIO'

3.4 use pod

Step 3.2 creates a local version of pod that doesn’t need a network, because the path we specified in Step 7 is local, so we can test it without relying on the network or GitHub.

  1. Go to the Phonercise root directory and run the following command:pod initThis will create a Podfile in the root directory, which will also be opened using text editing or vim
  2. Add code:pod 'ThreeRingControl', :path => '.. /ThreeRingControl'Here is a relative path, according to the path you put the two projects, we said at the beginning of the tutorial put the two projects in the same directory, so it is easier
  3. Save the configuration and exit. Run the following command in the root directory:pod install
  4. Instead of using the Phonercise. Project file to open your project, you’ll use the xcworkspace file to open it. You’re done

3.5 release pod

3.5.1 Creating a Warehouse

The POD is just a local file that you can’t share with your colleagues, let alone anyone else, so you’ll need to have a code pod to store your code in.

  1. You can put your code on an open source repository like GitHub
  2. You can also put it on your own company’s internal code base for internal use

Here’s GitHub as an example:

3.5.2 Uploading the Code

Open terminator, enter the ThreeRingControl directory and run the following command:

Git add. Git commit -m"initial commit"
git push
Copy the code

So your code is uploaded to GitHub

3.5.3 Tag and Lint tests

Tag it with the version number in your Podspec

Git tag 1.0.0 git pushCopy the code

In addition, Cocoapod provides a command to check whether your pod is properly written: Pod Spec Lint. If not, it will tell you what isn’t already written

3.5.4 update Podfile

Now that GitHub is set up, of course change it to GitHub’s address. Open Podfile on Phonercise and change the s.ource field to: s.source = { :git => ‘https://github.com/xxx/xxx.git’, :tag => s.version}’

Refers to the line command

Pod update // or POD installCopy the code

4 Framework generated by cocoapod

You remember the difference between Framework and open source library, Framework is more closed, you can only see the header file, you can’t see the implementation file, but we just uploaded the code to GitHub and it is completely open source.

What if we just need to build the Framework, hide the implementation, and not open source libraries? That’s not what we want!

Can we still use Cocoapod?

The answer is yes of course!!

AFNetworking. If you’ve already referenced AF, you’ll see that cocoapod has generated AFNetworking. Framework for you

Let’s right click and say Show in Finder

In fact, the Framework has been generated, as shown below

So does the Framework work? Of course!!

With this Framework you don’t need to add pod “AFNetworking”, “~> 3.1.0” to your podfile. You need to copy this file to your project and then add it to your project to use AFNetwork. Just add the Framework as we did in Step 2.3.2

So you can use it, and notice, we can’t see the source code, as shown here, it’s all.h files, no.m files. Isn’t that what you need

Instead of going to an open source site during step 3.5.1 to create the code base, we just upload the code to a private Git and then release the Framework automatically generated by Cocoapod.