SFBaseKit
SFBaseKit consists of commonly used utilities and extensions, not-contained in native iOS frameworks.
Table of contents
Features
- Coordinators - setup your navigation using Coordinator Pattern.
- Base Classes - useful subclasses that implement generally used utilities.
- Extensions - helpful extensions of UIKit components, data types and collections.
Requirements
- iOS 10.0+
- Swift 4
Installation
Cocoapods
SFBaseKit is available through CocoaPods. CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate SFBaseKit into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'SFBaseKit', '~> 2.1.0'
To get the full benefits import SFBaseKit
at the start of the source file:
import SFBaseKit
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate SFBaseKit into your Xcode project using Carthage, specify it in your Cartfile:
github "scalefocus/SFBaseKit" ~> 2.1.0
Usage Example
Coordinator
Make sure to follow the steps below in order to have properly setup coordinator pattern. First step under app target on the General tab. In the Deployment Info section, remove the entry in the Main Interface field.
Setup #1
AppCoordinator and window should be initialized in AppDelegate
var window: UIWindow?
var appCoordinator: Coordinator?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize the window and the appCoordinator
window = UIWindow(frame: UIScreen.main.bounds)
appCoordinator = AppCoordinator(window: window)
appCoordinator?.start()
return true
}
OR
Setup #2
Initialization of window and AppCoordinator should take place in SceneDelegate not in AppDelegate.
var window: UIWindow?
var appCoordinator: Coordinator?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
// Initialize the window and the appCoordinator
window = UIWindow(frame: UIScreen.main.bounds)
window.windowScene = windowScene
appCoordinator = AppCoordinator(window: window)
appCoordinator?.start()
}
In AppCoordinator the first coordinator should be initialized and added to childCoordinators.
let loginCoordinator = LoginCoordinator(navigationController: navigationController)
addChildCoordinator(loginCoordinator)
In AppCoordinator start method the first coordinator should be started.
childCoordinators.first?.start()
In LoginCoordinator override start method and push or present ViewController.
class LoginCoordinator: Coordinator {
override func start() {
guard let loginViewController = LoginViewController.instantiateFromStoryboard() else { return }
loginViewController.sceneDelegate = self
navigationController.pushViewController(loginViewController, animated: false)
}
}
To run the example project, clone the repo, and run pod install from the Example directory first.
Observable
ViewModel
Bindable property should be declared as Observable in ViewModel.
struct ExampleViewModel {
let name = Observable<String>("")
let email = Observable<String>("")
}
ViewController
To create an Observable binding - sink or sinkAndFire should be called in ViewController. To bind an Observable with UIControls - oneWayBind or twoWayBind should be called in ViewController.
@IBOutlet private weak var nameLabel: UILabel!
@IBOutlet private weak var emailTextField: UITextField!
override viewDidLoad() {
...
setupBinding()
}
private func setupBinding() {
viewModel.name.sink { [weak self] name in
self?.nameLabel.text = name
}
viewModel.email.oneWayBind(with: emailTextField)
}
License
SFBaseKit is available under the MIT license. See the LICENSE file for more info.