Latest 0.1.0
Homepage https://github.com/MihaelIsaev/SwifRootViewController
License MIT
Platforms ios 8.0
Authors

🍀Useful root navigation view controller (by best practice)

How to install

SwifRootViewController is available through CocoaPods.

To install it, simply add the following line in your Podfile:

pod 'SwifRootViewController', '~> 0.1.0'

How to use

Create a RootViewController.swift in your project and inherit it from SwifRootViewCtonroller like this

import UIKit
import SwifRootViewController

class RootViewController: SwifRootViewController<DeeplinkType> {
    // may be shown on app launch as initial view controller
    override var splashScreen: UIViewController {
        return SplashViewController() // replace with yours
    }

    // where user will be moved on `showLoginScreen`
    override var loginScreen: UIViewController {
        return LoginViewController() // replace with yours
    }

    // where user will be moved on `switchToLogout`
    override var logoutScreen: UIViewController {
        return LoginViewController() // replace with yours
    }

    // means your main view controller for authorized users
    override var mainScreen: UIViewController {
        return MainViewController() // replace with yours
    }

    // shows before main screen
    override var onboardingScreen: UIViewController? {
        // return something here to show it right after authorization
        return nil
    }

    // check authorization here and return proper screen
    override var initialScreen: UIViewController {
        return Session.shared.isAuthorized ? splashScreen : loginScreen
    }

    // handle deep links here
    override func handleDeepLink(type: DeeplinkType) {
        /// check you deep link in switch/case
        /// and go to the proper view controller
    }
}

If you don’t want to use deep links for now you could use SwifRootViewControllerSimple instead of SwifRootViewController<DeeplinkType>

Setting RootViewController as a window’s rootViewController
@UIApplicationMain
class AppDelegateBase: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if super.application(application, didFinishLaunchingWithOptions: launchOptions) {
        // needed to set `RootViewController` aswindow's  `rootViewController`
        RootViewController().attach(to: &window)
        // needed for deep links registration
        // ShortcutParser.shared.registerShortcuts()
        return true
    }
    return false
  }
}

Switching screens

First of all declare helpers in AppDelegate

extension AppDelegate {
    static var shared: AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }

    var rootViewController: RootViewController {
        return window!.rootViewController as! RootViewController
    }
}

Then use them from anywhere like this

AppDelegate.shared.rootViewController.showLoginScreen()
AppDelegate.shared.rootViewController.switchToMainScreen()
AppDelegate.shared.rootViewController.switchToLogout()

Deep links

Setting RootViewController as a window’s rootViewController

Implement ShortcutParser

import Foundation
import UIKit

enum ShortcutKey: String {
    case activity = "com.yourapp.activity"
    case messages = "com.yourapp.messages"
}

class ShortcutParser {
    static let shared = ShortcutParser()
    private init() { }

    func registerShortcuts() {
        let activityIcon = UIApplicationShortcutIcon(type: .invitation)
        let activityShortcutItem = UIApplicationShortcutItem(type: ShortcutKey.activity.rawValue, localizedTitle: "Recent Activity", localizedSubtitle: nil, icon: activityIcon, userInfo: nil)

        let messageIcon = UIApplicationShortcutIcon(type: .message)
        let messageShortcutItem = UIApplicationShortcutItem(type: ShortcutKey.messages.rawValue, localizedTitle: "Messages", localizedSubtitle: nil, icon: messageIcon, userInfo: nil)

        UIApplication.shared.shortcutItems = [activityShortcutItem, messageShortcutItem]
    }

    func handleShortcut(_ shortcut: UIApplicationShortcutItem) -> DeeplinkType? {
        switch shortcut.type {
        case ShortcutKey.activity.rawValue:
            return  .activity
        case ShortcutKey.messages.rawValue:
            return  .messages
        default:
            return nil
        }
    }
}

Implement DeepLinkManager

import Foundation
import UIKit

// List your deeplinks in this enum
enum DeeplinkType {
    case messages
    case activity
}

let Deeplinker = DeepLinkManager()
class DeepLinkManager {
    fileprivate init() {}

    private var deeplinkType: DeeplinkType?

    @discardableResult
    func handleShortcut(item: UIApplicationShortcutItem) -> Bool {
        deeplinkType = ShortcutParser.shared.handleShortcut(item)
        return deeplinkType != nil
    }

    // check existing deepling and perform action
    func checkDeepLink() {
        if let rootViewController = AppDelegate.shared.rootViewController as? RootViewController {
            rootViewController.deeplink = deeplinkType
        }

        // reset deeplink after handling
        self.deeplinkType = nil
    }
}

Configure AppDelegate

@UIApplicationMain
class AppDelegateBase: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if super.application(application, didFinishLaunchingWithOptions: launchOptions) {
        // needed to set `RootViewController` aswindow's  `rootViewController`
        RootViewController().attach(to: &window)
        // needed for deep links registration
        ShortcutParser.shared.registerShortcuts()
        return true
    }
    return false
  }

  override func applicationDidBecomeActive(_ application: UIApplication) {
    super.applicationDidBecomeActive(application)
    Deeplinker.checkDeepLink()
  }

  // MARK: Shortcuts

  func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    completionHandler(Deeplinker.handleShortcut(item: shortcutItem))
  }
}

Latest podspec

{
    "name": "SwifRootViewController",
    "version": "0.1.0",
    "summary": "Useful root navigation view controller",
    "description": "Useful root navigation view controller (best practice)",
    "homepage": "https://github.com/MihaelIsaev/SwifRootViewController",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "MihaelIsaev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/MihaelIsaev/SwifRootViewController.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "SwifRootViewController/**/*",
    "swift_version": "4.2"
}

Pin It on Pinterest

Share This