Search code examples
iosswiftnavigation

Swift 4 start with custom ViewController


I have chatting app. I decide to create SlideShow tutorial for it. Now I have problem. How can I run TutorialVC just once when user install the app?

Usually app starts with AuthVC. Now I want to run tutorialVC just once, and then when user close app and run it again, from auth like usually.

My tutorial VC:

class TutorialViewController: UIViewController, UIScrollViewDelegate {
    @IBAction func understandButtonAction(_ sender: Any) {
    }

    @IBOutlet weak var understandButton: UIButton!
    @IBOutlet weak var tutorialPageControl: UIPageControl!
    @IBOutlet weak var tutorialScrollView: UIScrollView!

    var images: [String] = ["1","2","3","4"]
    var frame = CGRect(x: 0, y: 0, width: 0, height: 0)

    override func viewDidLoad() {
        super.viewDidLoad()

        setup()
        addSlider()
        setupButton()
    }

    //===============================
    //EVTAuthorizationViewController
    //===============================
    override func viewWillAppear(_ animated: Bool) {
        UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
    }

    override func viewWillDisappear(_ animated: Bool) {
        UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal 
    }

    //AddButton
    func setupButton(){
        understandButton.layer.cornerRadius = 20
    }

    @IBAction func buttonAction(_ sender: Any?) {
        print("Successful")
    }

    //ScrollBars
    func setup(){
        self.understandButton.isHidden = true
        tutorialScrollView.showsHorizontalScrollIndicator = false
        tutorialScrollView.showsVerticalScrollIndicator = false
    }

    //ScrollView method
    //=============================
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
        tutorialPageControl.currentPage = Int(pageNumber)
        if tutorialPageControl.currentPage == 3{
            self.understandButton.isHidden = false
        }else{
            self.understandButton.isHidden = true
        }
    }

    //Addslider with photo
    func addSlider(){
        tutorialPageControl.numberOfPages = images.count
        for index in 0..<images.count{
            let xPos = self.view.frame.size.width * CGFloat(index)
            frame.origin.x = tutorialScrollView.frame.size.width * CGFloat(index)
            //frame.size = view.frame.size

            let imageView = UIImageView(frame: CGRect(x: xPos, y: 0, width: self.view.frame.width, height: self.view.frame.size.height))
            imageView.image = UIImage(named: images[index])
            imageView.contentMode = .scaleAspectFill
            self.tutorialScrollView.addSubview(imageView)
        }

        tutorialScrollView.contentSize = CGSize(width: (view.frame.size.width * CGFloat(images.count)), height: view.frame.size.height)
        tutorialScrollView.delegate = self
    }
}

a busy cat


Solution

  • Use userDefaults. I suppose the understandButton is the button the user hits to skip the tutorial, so when when it's tapped set a true bool value for a key that you are going to use, here I've chosen "tutorial presented":

    @IBAction func understandButtonAction(_ sender: Any) {
        UserDefaults.standard.set(true, forKey: "tutorial presented")
    }
    

    and when the app launches, in the AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        let window = (UIApplication.shared.delegate as! AppDelegate).window
        let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
        if UserDefaults.standard.bool(forKey: "tutorial presented") == true {
            let controller = storyboard.instantiateViewController(withIdentifier: "Your Navigation controller name")
            window?.rootViewController = tutorialViewController()
        } else {
            let tutorial = storyboard.instantiateViewController(withIdentifier: "Your tutorial controller name")
            window?.rootViewController = tutorial
        }
        window?.makeKeyAndVisible()
        return true
    }