To be honest, any of the features we use can be found on github and other open source sites with sample code or related open source libraries. I personally think we should be careful before using an open source code for our own projects:

  • There are some open source projects that for some reason the author doesn’t maintain them anymore, there are bugs and we have to maintain them ourselves or find new libraries to replace them;
  • Our requirements are different from open source libraries, and even if we use open source code, we need to modify it again.
  • After the iOS system is upgraded, the authors of some open source libraries cannot release new compatible versions in time, which may cause compatibility problems.
  • Apple has increased the size of non-Wi-Fi downloaded apps to 150 MEgabytes, but the introduction of a large number of third-party libraries in the project has inevitably resulted in a larger APP package than expected.

Although there are many open source libraries for some functions in engineering, such as LoadingView, network request, JSON to Model, ETC., I still suggest using systematic methods as far as possible, or encapsulating a component and making it open source. In this way, the above problems can be avoided, and the new project can be integrated into the new project more conveniently.

In general, there are a few steps to building an open source library:

  • Writing open source code
  • Test and modify
  • Publish to open source platforms such as Github
  • Later maintenance, including timely update, bug fixing, dealing with Issues and Pull requests, etc

1. Create projects and write codes

1. Log in to GitHub and create a repository named RPBannerView-Swift. Here I chose MIT Open Source protocol.

2. Create an rpBannerView-swift folder locally and clone the newly created code into the file

3. Create a new project in this folder named rpBannerView-Swift and upload the code to GitHub.

cd Desktop/RPBannerView-Swift/ git clone https://github.com/dengfeng520/RPBannerView-Swift.git git add ./ git commit -a -m "upload code" git push origin master git llCopy the code

4. Compatible with iOS versions

Start the rpBannerView-Swift project and change the minimum supported version to iOS 11. When you build the project, an error message is displayed. Make compatibility between AppDelegate and SceneDelegate.

  • in AppDelegate.swift
@available(iOS 13.0, *)
extension AppDelegate {
    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}
Copy the code
  • in SceneDelegate.Swift
@available(iOS 13.0, *) class SceneDelegate: UIResponder, UIWindowSceneDelegate {}Copy the code

2. Post to Carthage

  • 1. Create a framework

Create a new framework and name it RPBannerView.

  • 2. Modify after completionRPBannerView.frameworkThe lowest version is iOS 11,

Select the new RPBannerView.framework –> Build Settings –> iOS Deployment Target –> iOS 11

  • Add RPBannerView. Framework file to compile.

    Select rpBannerView. framework –> Build Phases to import the files you want to compile.

  • Xcode Schemes

Go to Manager Schemes –> Check shared

  • 5. Build the FrameWork

Switch to RPBannerView.framework, be careful not to select emulator!!

CD to the project file in Terminal, run build command,

cd Desktop/RPBannerView-Swift/
carthage build --no-skip-current
Copy the code

Under normal circumstances, the first build may report an error. We do not know the cause at present.

Skipped building RPBannerView-Swift due to the error: Dependency "RPBannerView-Swift" has no shared framework schemes

Open Manage Schemes, deselect RPBannerView, click Close, open again and deselect RPBannerView. The rebuild succeeded.

Building scheme "RPBannerView" in RPBannerView-Swift.xcodeproj

Carthage –> Build –> iOS –> RPBannerView. Framework is a file that has been compiled successfully.

  • 6, uploadRPBannerView-FrameWorkTo making

Once compiled, you need to upload the code to GitHub and open source it so that others can use the open source library through Carthage.

cd Desktop/RPBannerView-Swift/
git add ./
git commit -a -m "update code"
git push origin master
Copy the code
  • Create a tag

After uploading to GitHub, you need to create a tag to determine the version of the library.

CD Desktop/RPBannerView-Swift/ git tag 1.0 git push --tagsCopy the code
  • 8. Verify whether it is successful

Create a new project named RPBannerViewDemo, import the uploaded libraries in Cartfile, then update Carthage, if successful

cd RPBannerViewDemo
touch Cartfile
Vim Cartfile
github "dengfeng520/RPBannerView-Swift"
Esc -> :wq
carthage update --platform iOS --no-use-binaries
Copy the code

–no-use-binaries –no-use-binaries –no-use-binaries –no-use-binaries –no-use-binaries –no-use-binaries –no-use-binaries –no-use-binaries

If Carthage is already used in your project, simply update rpBannerView-Swift.

carthage update RPBannerView-Swift --platform iOS
Copy the code

After completing the update, open the project and select TARGETS –>Build Phases–> Link Binary With Libries. Select Add File –> Carthage –> Build –> iOS to Add rpBannerView. FrameWork.

/usr/local/bin/Carthage copy-frameworks
Copy the code

Next, click +, select New Run Script Phase, now create a New Run Script, add:

$(SRCROOT)/Carthage/Build/iOS/RPBannerView.framework
Copy the code

Then you can happily use your own open source libraries in your projects.

import RPBannerView

RPBanner.showBanner(BannerDisplay(title: "test loading..." ,backColor: UIColor.red, addView: view))
Copy the code

Publish to CocoaPods

  • Create rpBannerView-swift. podspec
cd Desktop/RPBannerView-Swift/
pod spec create RPBannerView-Swift
vim RPBannerView-Swift.podspec
Copy the code
  • 2. Edit rpBannerView-swift. podspec
Pod: : Spec. New do | Spec | Spec. The name = "RPBannerView - Swift" Spec. Version = "1.0" Spec. The summary = "A short description of RPBannerView." spec.description = "RPBannerView-Swift" spec.homepage = "https://github.com/dengfeng520/RPBannerView-Swift" spec.license = { :type => "MIT", :file => "LICENSE" } spec.author = { "dengfeng520" => "[email protected]" } spec.platform = :ios The spec. The ios. Deployment_target = "11.0" spec. Source = {: git = > "https://github.com/dengfeng520/RPBannerView-Swift", :tag => "#{spec.version}" } spec.source_files = "RPBannerView-Swift/RPBannerView/*.swift" spec.framework = "Foundation",  "UIKit" endCopy the code
  • 3. Save the PodSpec file and verify
pod spec lint RPBannerView-Swift.podspec --allow-warnings
Copy the code

Verify a successful commit to the CocoaPods/Specs codebase so that others can install the open source library via the pod Install command.

Verification success prompt:

RPBannerView-Swift.podspec passed validation. 
Copy the code

If you have not registered CocoaPods accounts, run the following command to register CocoaPods accounts:

pod trunk register [email protected] 'dengfeng520' --description='dengfeng520'

Copy the code

View CocoaPods account information:

pod trunk me
Copy the code
  • Publish to CocoaPods

If already registered, publish directly to CocoaPods server:

pod trunk push RPBannerView-Swift.podspec --allow-warnings
Copy the code

Update CocoaPods local library after successful publication:

 pod setup
Copy the code

And then the search

pod search RPBannerView-Swift
Copy the code
  • 5. Verify whether it can be used in the project
cd RPBannerViewDemo
pod init
Vim Podfile
pod 'RPBannerView-Swift', :git => 'https://github.com/dengfeng520/RPBannerView-Swift'
Esc -> :wq
pod install

Copy the code

Once the POD is successfully downloaded, you can use the library in your project.

4. Write the readme.md file

As shown in the figure, an open source readme. md consists of three parts:

  • (1) the Banner at the top

Here is a reference to other open source projects to add a Logo picture and project name, the picture and text are all in the center.

<div align=center>
 <img src="https://github.com/dengfeng520/RPBannerView-Swift/blob/master/Banner.png?raw=true" width = 314 height = 200/>
</div>
<h2 align="center">RPBannerView</h2>
Copy the code
  • (2) Added Badge

    • Some tags about this library
    • Code test coverage

    See blog: GitHub Project Badges added and set

Code coverage is an important indicator of the code quality of an open source project. For open source code with the same function, people generally prefer the code with high coverage. It is recommended here to use codecov’s online service-generated code test coverage Badge.

  • (3) Project Description

For the specification section, the problem should be solved so that others can choose your code in the shortest time, generally the first is to quickly solve the developer’s confusion. What kind of library is this? What are the advantages over other similar libraries? How to get started quickly?

We can also add a few other things later, such as release history, project improvement plan, detailed usage guide, etc.

5, late maintenance

After Posting your code to Github, you should maintain it regularly, including bug fixes, version updates, Issues and Pull requests, and undiscovered Issues and bugs.


Related link: Github rpBannerView-Swift