Thursday, February 14, 2019

How best to create static functions

///////////////////////////////
In Struct:

struct MyStruct {
    static func something() {
        println("Something")
    }
}
Called via:
MyStruct.something()

///////////////////////////////
In Class

class MyClass {
    class func someMethod() {
        println("Some Method")
    }
}
called via:

MyClass.someMethod()

Tuesday, February 12, 2019

How to check what is inside a Assets.car file

xcrun --sdk iphoneos assetutil --info

EXAMPLE OUTPUT:

 {
    "AssetType" : "Image",
    "BitsPerComponent" : 8,
    "ColorModel" : "Monochrome",
    "Colorspace" : "gray gamma 22",
    "DeploymentTarget" : "2018",
    "Idiom" : "universal",
    "Image Type" : "kCoreThemeOnePartScale",
    "Name" : "bluetooth_on",
    "Opaque" : false,
    "PixelHeight" : 54,
    "PixelWidth" : 54,
    "RenditionName" : "baseline_bluetooth_black_18pt_3x.png",
    "Scale" : 3,
    "SizeOnDisk" : 338,
    "Template Mode" : "template"
  },
  {
    "AssetType" : "Image",
    "BitsPerComponent" : 8,
    "ColorModel" : "Monochrome",
    "Colorspace" : "gray gamma 22",
    "DeploymentTarget" : "2018",
    "Idiom" : "universal",
    "Image Type" : "kCoreThemeOnePartScale",
    "Name" : "keypad_black_off",
    "Opaque" : false,
    "PixelHeight" : 168,
    "PixelWidth" : 168,
    "RenditionName" : "keypad_black_off@3x.png",
    "Scale" : 3,
    "SizeOnDisk" : 338,
    "Template Mode" : "automatic"
  },

Monday, February 11, 2019

How to cleanly launch VC using storyboard


enum AppStoryboard : String {
    case Main = "Main"
    case PreLogin = "PreLogin"
    case Timeline = "Timeline"
    var instance : UIStoryboard {
      return UIStoryboard(name: self.rawValue, bundle: Bundle.main)
    }
}
// USAGE :

let storyboard = AppStoryboard.Main.instance

// Old Way

let storyboard = UIStoryboard(name: “Main”, bundle: Bundle.main)

https://medium.com/@gurdeep060289/clean-code-for-multiple-storyboards-c64eb679dbf6

Friday, February 8, 2019

How to create an app with access control management

https://medium.com/ios-os-x-development/access-control-management-with-swift-cc3c3d68cbc3

What are the View Controller's lifecycle events

# Controller Lifecycle events order ?
There are a few different lifecycle event

- loadView
Creates the view that the controller manages. It’s only called when the view controller is created and only when done programatically. It is responsible for making the view property exist in the first place.

- viewDidLoad
Called after the controller’s view is loaded into memory. It’s only called when the view is created.

- viewWillAppear
It’s called whenever the view is presented on the screen. In this step the view has bounds defined but the orientation is not applied.

- viewWillLayoutSubviews
Called to notify the view controller that its view is about to layout its subviews. This method is called every time the frame changes

- viewDidLayoutSubviews
Called to notify the view controller that its view has just laid out its subviews. Make additional changes here after the view lays out its subviews.

- viewDidAppear
Notifies the view controller that its view was added to a view hierarchy.

- viewWillDisappear
Before the transition to the next view controller happens and the origin view controller gets removed from screen, this method gets called.

- viewDidDisappear
After a view controller gets removed from the screen, this method gets called. You usually override this method to stop tasks that are should not run while a view controller is not on screen.

- viewWillTransition(to:with:)
When the interface orientation changes, UIKit calls this method on the window’s root view controller before the size changes are about to be made. The root view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.`

How to add a path when saving data to the documents folder

let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory, in: .userDomainMask)
let docsURL = dirPaths[0]

let newDir = docsURL.appendingPathComponent("data").path
do {
    try filemgr.createDirectory(atPath: newDir,
                withIntermediateDirectories: true, attributes: nil)
    } catch let error as NSError {
            print("Error: \(error.localizedDescription)")
}

How to create a Singleton

class Environment: NSObject {
    private var values = [String:String]()
    let skipLogin:Bool
    let baseURL:String
    let baseAuthURL:String

    class var shared: Environment {
        struct Singleton {
            static let instance = Environment()
        }
        return Singleton.instance
    }
}  

How to create a simple animation

https://blog.usejournal.com/ios-animations-uiview-part-1-d94305bee2f5

Thursday, February 7, 2019

How to create a custom cell with callback when row selected

custom cell:

import UIKit

typealias ActionCallback = () -> Void

class ActionLinkCell: UITableViewCell {
    var cb: ActionCallback? = nil

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifier)
        self.accessoryType = .disclosureIndicator
        self.textLabel?.textColor = self.tintColor
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ActionLinkCell.handleIsTap(_:)))
        self.addGestureRecognizer(tapGesture)
        
    }

    @objc func handleIsTap(_ sender: UIGestureRecognizer) {
        debugPrint("\(#function)")
        if let callback = self.cb {
            callback()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func config(title: String, callback: ActionCallback? = nil) {
        self.textLabel?.text = title
        self.setNeedsDisplay()
        self.cb = callback
    }

}

calling code:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier:    bluetoothActionLinkCellId, for: indexPath) as! ActionLinkCell
    let message = NSLocalizedString("Pair device using Bluetooth", comment: "")
    cell.config(title: message) {
    //MY CODE GOES HERE
    }
    return cell
}