Swiftpack.co - Package - squareboat/swift-utilities-package


Utilities is a Swift package of almost all the utility functions needed to build amazing iOS Applications


Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding Utilities as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/Root-vb/swift-utilities-package.git", .upToNextMajor(from: "1.0.1"))


import Utilities

/* Networking */
NetworkUtils.isInternetAvailable() // returns true or false depending upon the device internet connection status

/* Validators Example */
Validators.isValidEmailAddress(emailId: "example@squareboat.com") // returns true

/* Calendar Example */
CalendarUtils.getTimeDifferenceString(timestampString: "1598255182") // returns something like 3 weeks ago

/* Network Image with caching. Extension method on UIImageView */
imageView.setImage(withUrl: url, placeholderImage: UIImage(named: "placeholder"), showIndicator: true)

Extension Methods


class func documentsDirectory() -> String

class func cachesDirectory() -> String


var htmlToAttributedString: NSAttributedString?

var htmlToString: String

var wordCount: Int


func addAction(title: String, style: UIAlertAction.Style = .default, isEnabled: Bool = true, handler: ((UIAlertAction) -> Void)? = nil) -> UIAlertAction

func addTextField(text: String? = nil, placeholder: String? = nil, editingChangedTarget: Any?, editingChangedSelector: Selector?)

convenience init(title: String, message: String? = nil, defaultActionButtonTitle: String = "OK", tintColor: UIColor? = nil)


var displayName: String?

var buildNumber: String?

var version: String?


func leftImage(image: UIImage, renderMode: UIImage.RenderingMode = .automatic)

func rightImage(image: UIImage, renderMode: UIImage.RenderingMode)

func setImageForAllStates(_ image: UIImage)

func setTitleColorForAllStates(_ color: UIColor)

func setTitleForAllStates(_ title: String)

func centerTextAndImage(imageAboveText: Bool = false, spacing: CGFloat)


convenience init?(hex: String)


func compressed(quality: CGFloat = 0.5) -> UIImage?

func compressedData(quality: CGFloat = 0.5) -> Data?

func cropped(to rect: CGRect) -> UIImage

func pngBase64String() -> String?

func jpegBase64String(compressionQuality: CGFloat) -> String?

convenience init(color: UIColor, size: CGSize)

convenience init?(base64String: String, scale: CGFloat = 1.0)


func blur(withStyle style: UIBlurEffect.Style = .light)

func blurred(withStyle style: UIBlurEffect.Style = .light) -> UIImageView

func setImage(withUrl url: URL, placeholderImage placeholder: UIImage?, showIndicator: Bool = false)

func setImage(withUrlString urlString: String, placeholderImage placeholder: UIImage?, showIndicator: Bool = false)


func getHeight(forWidth width: CGFloat) -> CGFloat


var isEmpty: Bool

var trimmedText: String?

var hasValidEmail: Bool

var hasValidPhoneNumber: Bool

@IBInspectable var leftViewTintColor: UIColor?

@IBInspectable var rightViewTintColor: UIColor?

func clear()

func setPlaceHolderTextColor(_ color: UIColor)

func addPaddingLeft(_ padding: CGFloat)

func addPaddingRight(_ padding: CGFloat)

func addPaddingLeftIcon(_ image: UIImage, padding: CGFloat)

func addPaddingRightIcon(_ image: UIImage, padding: CGFloat)

UITableViewCell and UICollectionView Cells

// Usage
extension MyTableViewCell: ReuseIdentifiable {}

MyTableViewCell().reuseIdentifier // returns "MyTableViewCell" as a String


@IBInspectable var borderColor: UIColor?

@IBInspectable var borderWidth: CGFloat

@IBInspectable var cornerRadius: CGFloat

var screenshot: UIImage? // If applicable

@IBInspectable var shadowColor: UIColor?

@IBInspectable var shadowOffset: CGSize

@IBInspectable var shadowOpacity: Float

@IBInspectable var shadowRadius: CGFloat

@IBInspectable var masksToBounds: Bool

var parentViewController: UIViewController?

func roundCorners(_ corners: UIRectCorner, radius: CGFloat)

func addShadow(ofColor color: UIColor = UIColor(red: 0.07, green: 0.47, blue: 0.57, alpha: 1.0), radius: CGFloat = 3, offset: CGSize = .zero, opacity: Float = 0.5)

func setDashedBoder(color: UIColor = .lightGray)

func addSubviews(_ subviews: [UIView])

func fadeIn(duration: TimeInterval = 1, completion: ((Bool) -> Void)? = nil)

func fadeOut(duration: TimeInterval = 1, completion: ((Bool) -> Void)? = nil)


func addNotificationObserver(name: Notification.Name, selector: Selector)

func removeNotificationObserver(name: Notification.Name)

func removeNotificationsObserver()

func showAlert(title: String?, message: String?, buttonTitles: [String]? = nil, highlightedButtonIndex: Int? = nil, completion: ((Int) -> Void)? = nil) -> UIAlertController

func addChildViewController(_ child: UIViewController, toContainerView containerView: UIView)

func removeViewAndControllerFromParentViewController()

func showLoader(color: UIColor? = nil)

func hideLoader()

func openShareActivity(itemsToShare : [Any])

func openSafariWithUrl(urlString : String)

Utility Methods


class func getNumberOfDaysBetweenTwoDates(date1 : Date, date2 : Date) -> DateComponents

public class func getActualDifferenceBetweenTwoDates(date1 : Date, date2 : Date) -> DateComponents

class func getDateAfterSomedays(daysToAdd : Int, dateToAdd : Date!) -> Date!

class func getTimeDifferenceString(timestampString : String) -> String


func decode<T>(_ type: T.Type, from json: [String:Any]) throws -> T where T: Decodable

// Usage Example
struct Example: Decodable {
    // ...

let dictionay = [

do {
    let decodedExample = try DictionaryDecoder().decode(Example.self, from: dictionary) /// return an object
} catch (let decodingError) {


func encode<T>(_ value: T) throws -> [String:Any] where T: Encodable

// Usage Example
struct Example: Encodable {
    // ...

let example = Example() /// Encodable object

do {
    let dictionary = try DictionaryEncoder().encode(example) /// return a dictionary
} catch (let encodingError) {


class func getImageHeightUsingAspectRatio (aspectRatio: Double, width: CGFloat)


class func convertToDictionary(text: String) -> [String: Any]?

class func convertToString(dictionary : Any) -> String?

class func getJsonStringFromArrayResponse(response: NSArray) -> Data?

func getJsonStringFromDictResponse(response: NSDictionary) -> Data?


class func isInternetAvailable() -> Bool


class func getBottomBarForSegmentControl(SCView : UISegmentedControl, color: UIColor) -> UIView


class func debounce(interval: Int, queue: DispatchQueue, action: @escaping (() -> Void)) -> () -> Void

class func delay(delay:Double, perform: @escaping () -> Void)


class func isValidEmailAddress(emailId : String) -> Bool

class func isValidPhoneNumber(number : String) -> Bool

class func isValidURL(urlString: String?) -> Bool

class func checkForIntAndFloat(data : Any) -> Any


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.


Utilities is released under the MIT license. See LICENSE for details.


Stars: 1

Used By

Total: 0