-2

i can add WalkthroughView on UIViewController pages but how i can add WalkthroughView on appDelegate?

 override open func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let defaults = UserDefaults.standard
    let hasViewedWalkthrough = defaults.bool(forKey: "hasViewedWalkthrough")

    if !hasViewedWalkthrough {
       //
        print("succes")
        //
       if let pageVC = storyboard?.instantiateViewController(withIdentifier: "WalkthroughViewController") as? WalkthroughViewController {
           present(pageVC, animated: true, completion: nil)
        }
   }
}
a.masri
  • 2,439
  • 1
  • 14
  • 32
felon
  • 183
  • 1
  • 4
  • 10

2 Answers2

1

Perhaps you mean one of the options?

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

        let rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "WalkthroughViewController")
        window = UIWindow(frame: UIScreen.main.bounds)

        window?.rootViewController = rootViewController
        window?.makeKeyAndVisible()

        return true
    }
}

Or:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "WalkthroughViewController")
             window?.rootViewController?.present(viewController, animated: true, completion: nil)
        }

        return true
    }
}
ViR
  • 276
  • 1
  • 3
  • second code worked but i need to show "WalkthroughView" just for one time. – felon Jun 14 '18 at 13:22
  • my mean is just one time after install app on device – felon Jun 14 '18 at 13:23
  • 1
    Use bool flag in UserDefaults to check whether the screen or application was already running. Example: UserDefaults.standard.set(true, forKey: "FirstRun") after run ViewController, if UserDefaults.standard.bool(forKey: "FirstRun") == false { } to present ViewController – ViR Jun 14 '18 at 13:30
  • i can't understand , how? please put full code as answer – felon Jun 14 '18 at 20:59
-1

You can use the app delegate for this

func application(_ application: UIApplication, didFinishLaunchingWithOptionslaunchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70