Skip to main content

Creating Swift Packages

Lets create our first Swift Package !

 iOS Developer level : Intermediate

 

Configuration when this article was written

 

Xcode : Version 11.5

MacOS : 10.15.5 (Catalina)

 

A working example

Final code can be found in the Resources section below.  



What are Swift Packages ?

 

Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that’s easy to use in your app’s project.



Enough of introduction , let’s be technical…



From Xcode 11, we can directly create Swift Package.

To ensure our package is rightly developed we will follow the steps mentioned below.

Step 1 : We create a single view application , we will have a service (which we later export as swift package) developed here

Step 2 : Create Swift Package with the same  single view application just developed, Move our service developed from our app to package.

Step 3 : Once we find our service working. Lets host this swift package on GitHub and Tag it.

Step 4: Create a new single view application from xcode and add Swift Package dependency from Remote (Github where we hosted.) 

This is all we need to do.. So let us get steps rolling...


Step 1 : Create our service with a single view app

Go to get xcode  and create a single view application and name it as MySwiftPackageCreator



Add a swift file and name it as DateFormat.swift

Add the following code to DateFormat.swift


public class DateFormat {

    public static func formatDate(dateToFormat  date : Date) -> String {

        let dateFormat = DateFormatter()

        dateFormat.dateFormat = "YYYY-MM-dd"

        return dateFormat.string(from: date)

    }

}


The class and method is marked public because we will have this file bundled in swift package and the other modules later accessing our swift package will have access to this.


Also formatDate is simple method to format a date passed into "YYYY-MM-dd" format.


Now in change the viewDidLoad() in ViewController.swift to look like below


 override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        print("Formatted date is \(DateFormat.formatDate(dateToFormat: Date()))")

 }


Now build the Xcode project . You will see Formatted date is 2020-08-15 printed on xcode console. Bingo our service is ready.



Step 2 : Create Swift Pacakage


Now Add new Swift Package to the same project. To do this follow the screenshots below.






Name the package as TTDateFormatteer. Also select your MySwiftPackageCreator project for options Add to: and Group: as show below.



Finally your project structure should look like the below screenshot




Now open Package.swift you should see the following lines

 .library(

            name: "TTDateFormatter",

            targets: ["TTDateFormatter"]),




So the name in .library is place where all the package files are present. Do not change this name.



Now delete the file TTDateFormatter.swift in TTDateFormatter folder.


Drag the file DateFormat.swift into TTDateFormatter folder. Also copy to folder



Now build the project yow will see the error "Use of unresolved identifier 'DateFormat'" in ViewController.swift



This is because 'DateFormat' is now moved to Swift Package.



Add  import TTDateFormatter, in ViewController.swift.



Now the error should go away. if the error persists change the target to TTDateFormatter and rebuild the xcode project.





Now select TTDateFormatter package from the project navigator parallelly hold option key from the keyboard and drag the package to desktop. This ensures you drag the entire package.


Step 3 :  Add Package to Github


Now create a repository named TTDateFormatter on your Github. Do not add any 

README.md file.


Now open terminal , Cd into TTDateFormatter folder on desktop.

Follow the commands below to push the Package to Github. (If comfortable push the package from xcode itself).


Now in terminal once you are in TTDateFormatter folder

git init

git add .

git commit -m "Swift package TTDateFormatter"

git remote add origin https://github.com/TeaTalkInternal/TTDateFormatter.git

// Here add your repository URL

git push -u origin master

git tag 1.0.0


git push --tags    



Now we  have our TTDateFormatter package on Github.




Step 4 :  Create a sample app to test our package

Go to get xcode  and create a single view application and name it as MyPackageTestFromRemote

Xcode -> New Project -> Single View App -> MyPackageTestFromRemote



Now go to project genral settings screen and click on add framework button 





In the libraries popup click on Add other, then select Add Package Dependency.





In the popup showing place to enter Package Repository Url add the url where SwiftPackage is hosted for me it is 
https://github.com/TeaTalkInternal/TTDateFormatter.git



Now the package is added .



Now open ViewController.swift change the code to look like below.


import UIKit

import TTDateFormatter


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        print("Formatted date is \(DateFormat.formatDate(dateToFormat: Date()))")

    }

}



Run and see the result. You will see the Formatted date is 2020-08-15 printed the console.


Yes we successfully created swift package and used it too.

 



Comments

Post a Comment

Popular posts from this blog

How to create your cocoapods?

Lets write our cocoapod and publish it to cocoapods.org    iOS Developer level :  Intermediate   Configuration when this article was written   Xcode :  Version 11.5 MacOS :  10.15.5 (Catalina)   A working example   Final code can be found in the Resources section below.     What is a cocoapod ?         CocoaPods  is an  application level dependency manager  for the  Objective-C ,  Swift  and any other languages that run on the Objective-C runtime, such as  RubyMotion , that provides a standard format for managing external  libraries.            CocoaPods focuses on source-based distribution of third party code and automatic integration into Xcode projects. Enough of introduction , let’s be technical… So to start let us create a framework using xcode.   Xcode -> New -> Project -> Framework   Name It as TTDeviceIdentifier   Inside create a file called DeviceIdentifier   Add following code   import  UIKit   public   struct  DeviceIdentifier {          public   static   func  get

iTunes (Apple App Store) release Pre-Requisites

 iTunes (Apple App Store) release Pre-Requisites  This is small note on details to be kept ready before you release you app ( .ipa file ) to apple app store. Following details you need to have in-hand before releasing app to Apple App Store.   Name of the App Privacy Policy URL  Promotional Text  Keywords (This will make you app more available when customers search on app store) Description Support URL Keywords  Marketing URL Copyright App Store Icon( 1024 px X 1024 px ) App Screenshots (Minimum 3),Sizes are mentioned below. (Please ask this with your UI Designer)     For Review Process   Test  Account Credentials  (This is for Apple to run through the app) 1.      Email  2.      Password     Contact Information    The person in your organisation who should be contacted if the App Review team has any questions or needs additional information.   1.      First Name 2.      Last Name 3.      Email 4.      Phone Number App Screenshots Sizes   iPhone screenshot sizes: iPhone 3 (3.5 Inch): 9

Helper Sites to build your Mobile App

Helper Sites to build your Mobile App ( Primary Stuff ) Idea to App   Following are the links , supporting your journey in transforming an idea into an app   Wireframes Have the screens you want  your  app to a have drawn on paper. Then have the photos of each screen clicked individually , Then use POP  https://marvelapp.com/pop/   . This  app helps you have your wireframes connected, just like an mock app.   Color Theme To decide on the color for your apps use   https://colorhunt.co/   https://www.materialpalette.com/ Visual Design To refer already existing designs use  https://pttrns.com/  , To design VD use  https://www.uxpin.com/   Free icons  (Free for commercial use) https://www.flaticon.com/ https://icons8.com/ https://www.iconfinder.com/free_icons Background Images   use   http://www.resplashed.com/     App Icons   To create app icons use  https://makeappicon.com/   App Store Screen Shots  https://theapplaunchpad.com   http://sketchtoappstore.com/   App Landing Page   To create