Adding Swift Package Dependencies to iOS App

Tram Ho

Introduce

In programming in general and iOS programming in particular, the use of third-party libraries is very common, allowing rapid development of applications by reusing parts of code that others have written previously. Most programming languages ​​today have tools to help manage these libraries in a focused and efficient manner, called dependency managers or package managers . When working with the Swift language, we are very familiar with 2 dependency managers, Cocoapods and Carthage . Today I will introduce you one more thing which is Swift Package Manager , it has been introduced since Swift 3.0 and until now it has been officially integrated into Xcode 11 by Apple , and in this article we will together Learn how to add Swift Package Dependencies into your iOS app.

Adding Swift Package Dependencies to iOS App

Overview

Because the Swift Package Manager is officially integrated into Xcode 11 by Apple , managing our dependencies will be simpler and less complicated than when we use Cocoapods or Carthage. We can easily check to see if the library we have used is the latest or the version version … And it also supports both public and private repositories .

Add a Package Dependency

To add your Swift Package Manager * Xcode project, choose File> Swift Packages> Add Package Dependency and enter its URL:

Then select the version or specific commit you want to use, then click next

As above, I have successfully added Alamofire into the iOS project, and together tested:

After successfully adding the dependency, you can also edit or delete it easily.

Compared to other package managers, like Cocoapods, you have to declare the library with the version you want and then execute the terminal to install that dependency into your project, which is quite troublesome. For Swift Package Manager, it is completely done on Xcode ?

Tips

  • When teaming up on a project, make sure everyone uses the same version of the package dependency. When you add the package dependency to the project, Xcode will generate the Package.resolved file. This file ensures that everyone uses the same version of the package dependency. You can find the Package.resolved file in the .xcodeproj => [appName] .xcodeproj / project.workspace / xcshareddata / swiftpm / Package.resolved directory . It will take the form:

  • If you want to update your dependency to the latest, select File> Swift Packages> Update To Lastest Versions, your library will have the latest updates.

Conclude

Above I together with you to learn about an old and new package manager in iOS programming. I find that Swift Package Manager is supported directly from Xcode, simpler to manage versions than Cocoapods and Carthage. In the near future, when most of the Dependency support Swift Package Manager , I believe it will replace the other package managers . In the next article, I will show you how to create a Swift Package Manager . Thanks for reading the article ✌️

Refer

Share the news now

Source : Viblo