0

hi I followed some tutorial in stack for only seeing UIPageviewcontroller running at first time I find the best one and than implement it in app delegate .... first of all I have only one storyboard with two viewcontroller which first one is my PageViewController and second one is my Loginvc I implemented this code on appdelegate and I get crash everything looks good but I get crash this is the code that I have implement it

var window: UIWindow?
var story : UIStoryboard?


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

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let lunchedBefore = UserDefaults.standard.bool(forKey: "lunchedBefore")

    if lunchedBefore {
        story = UIStoryboard(name: "TShopUI", bundle: nil)
        let rootcontroller = story?.instantiateViewController(withIdentifier: "LoginVC")
        if let window = self.window {
            window.rootViewController = rootcontroller
        }
    } else {

        UserDefaults.standard.set(true, forKey: "lunchedBefore")
        story = UIStoryboard(name: "TShopUI", bundle: nil)
        let rootcontroller = story?.instantiateViewController(withIdentifier: "MainVC")

        if let window = self.window {
            window.rootViewController = rootcontroller
        }

    }
    return true
}

thanks for every help please answer as clear as you can I'm new to iOS development

Shabbir Ahmad
  • 615
  • 7
  • 17
Javad
  • 43
  • 7

2 Answers2

2

It's crashing because of UserDefaults, you have to handle UserDefaults like below,

Updated: instead of Bool we can use object. So Whether the if condition satisfies, it's not a first launch. else it's first launch.

var window: UIWindow?
var story : UIStoryboard?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.statusBarStyle = .lightContent
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    if let lunchedBefore = UserDefaults.standard.object(forKey: "lunchedBefore") {
        story = UIStoryboard(name: "TShopUI", bundle: nil)
        let rootcontroller = story?.instantiateViewController(withIdentifier: "LoginVC")
        if let window = self.window {
            window.rootViewController = rootcontroller
        }
    } else {
        UserDefaults.standard.set(true, forKey: "lunchedBefore")
        story = UIStoryboard(name: "TShopUI", bundle: nil)
        let rootcontroller = story?.instantiateViewController(withIdentifier: "MainVC")
        if let window = self.window {
            window.rootViewController = rootcontroller
        }
    }
    return true
}
Austin Michael
  • 460
  • 4
  • 18
  • thanks for help but it will give me this error Initializer for conditional binding must have Optional type, not 'Bool' – Javad Aug 23 '17 at 10:54
  • add the answer that I added you answer is true but it should add storyboard id – Javad Aug 23 '17 at 20:29
0

the problem was that I should set a storyboard ID for each view controller and than in delegate set their identifier

Javad
  • 43
  • 7