UIPageViewController. Create and Skip App Tutorial Pages.

Earlier, I have created a short video tutorial on how to use UIPageViewController to create the “App Tutorial Page” which will start up as soon as your mobile app launches. You can use this app tutorial to introduce your app to the user who starts your app for the very first time and then let them learn a little bit about your app and how to use it.

The next logical step for my previous video tutorial was to create a video tutorial on how to skip an app tutorial page and how to not show it any more if user taps on a “Do not show” or a “Skip” button.

So, from this new video tutorial I am sharing with you today you can learn how to remember user’s choice when they tap on a “Skip” or “Do not show” button and how to not display app tutorial pages when user starts your app the next time.

UIPageViewController -Create App Tutorial Pages. Video.

UIPageViewController – Skip App Tutorial Pages the Next Time User Starts the App.

UIPageViewController – Create and Skip App Tutorial Pages. Source Code.

You can download source code from Github here: https://github.com/simplyi/UIPageVIewControllerExample or copy pieces of code from the code below:

AppDelegate.Swift file

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        let defaults = UserDefaults.standard
        let skipTutorialPages = defaults.bool(forKey:"skipTutorialPages")
        
        if skipTutorialPages
        {
          let mainStoryBoard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
            
          let nextView: NextViewController = mainStoryBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
            
            window?.rootViewController = nextView
            
        } else {
            UIPageControl.appearance().pageIndicatorTintColor = UIColor.lightGray
            UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.red
        }
        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

ViewController that holds App Tutorial Pages

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

    var pageViewController:UIPageViewController!
    var pageImages: NSArray!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        
        pageImages = NSArray(objects: "screen1","screen2","screen3")
        
        self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyPageViewController") as! UIPageViewController
        
        
        self.pageViewController.dataSource = self
        
        let initialContenViewController = self.viewControllerAtIndex(0) as ContentViewController
 
        self.pageViewController.setViewControllers([initialContenViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
        
        self.pageViewController.view.frame = CGRect(x: 0, y: 100, width: self.view.frame.size.width,  height: self.view.frame.size.height-100)
        
        
        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMove(toParentViewController: self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
    func viewControllerAtIndex(_ index: Int) ->ContentViewController
    {
  
        let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
        
        pageContentViewController.imageFileName = pageImages[index] as! String
        pageContentViewController.pageIndex = index
        
        
        return pageContentViewController
        
    }
    
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    {
        let viewController = viewController as! ContentViewController
        var index = viewController.pageIndex as Int
        
        if(index == 0 || index == NSNotFound)
        {
            return nil
        }
        
        index -= 1
        
        return self.viewControllerAtIndex(index)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        let viewController = viewController as! ContentViewController
        var index = viewController.pageIndex as Int
        
        if((index == NSNotFound))
        {
            return nil
        }
        
        index += 1
        
        if(index == pageImages.count)
        {
            return nil
        }
        
        return self.viewControllerAtIndex(index)
    }
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int
    {
        return pageImages.count
    }
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int
    {
        return 0;
    }
    
    @IBAction func skipButtonTapped(_ sender: AnyObject) {
     
        let defaults = UserDefaults.standard
        defaults.setValue(true, forKey:"skipTutorialPages")
        defaults.synchronize()
        
        let nextView: NextViewController = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
        
       let appDelegate = UIApplication.shared.delegate as! AppDelegate
        
       appDelegate.window!.rootViewController = nextView
 
    }
}