1

I am trying to put a first launch view. I already tried some stuff but that won't work.

Here is what I have:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageControl.backgroundColor = [UIColor whiteColor];

    return YES;

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yourCondition"])
    {
        //launch your first time view
        self.viewController = [[viewController alloc] initWithNibName:@"viewController" bundle:nil];
    }
    else
    {
        //launch your default view
        self.viewController = [[viewController alloc] initWithNibName:@"defaultViewController" bundle:nil];

        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"yourCondition"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

}

Somehow that doesn't work it says that viewController is not found as an object type of AppDelegate. Any Ideas?

Niclas
  • 89
  • 9
  • 5
    One problem is that no code after `return YES` will execute - you've returned already! – matt May 20 '14 at 17:52
  • @matt Oh ok, I have put the `return YES` to the end. Thank you! – Niclas May 20 '14 at 17:55
  • Are you thinking of a launch image, rather than your app's first view? – Aaron May 20 '14 at 17:59
  • @aaron I am thinking of an existing view Controller wich should give the user a little tour of the App. I have finished making the Tour but I need to know how to only display the view Controller on the first launch of the App. – Niclas May 20 '14 at 18:04

3 Answers3

3

If you do nothing, the storyboard's initial view controller will appear. If you want something else to happen, what you want to set is self.window.rootViewController. In that case you will also need to create and show the window manually.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Here is an example of how to get an app started when there is no storyboard: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p239nibViewController/ch19p575manualViewController/AppDelegate.m – matt May 20 '14 at 17:56
  • The link seems to be dead – SwiftsNamesake Jan 06 '18 at 01:40
0

There are a few things wrong with your implementation of application:didFinishLaunchingWithOptions: which is most definitely the cause of your troubles.

I recommend that you read through a few tutorials on getting started with iOS development as these are fundamental concepts you will need to grasp if you intend on doing any serious development in the future.

That being said, the following code should see you right:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];

    UIViewController *viewController = nil;

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yourCondition"])
    {
        //launch your first time view
        viewController = [[viewController alloc] initWithNibName:@"viewController" bundle:nil];
    }
    else
    {
        //launch your default view
        viewController = [[viewController alloc] initWithNibName:@"defaultViewController" bundle:nil];

        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"yourCondition"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [[self window] setRootViewController:viewController];

    [[self window] makeKeyAndVisible];

    return YES;
}

The default implementation of application:didFinishLaunchingWithOptions: expects that you return a boolean, which is indicated by the method signature.

You are also missing the important call at the beginning of the method which sets up the main NSWindow for your application to be presented.

As pointed out by @matt, your code expects that you have a property on you AppDelegate which would hold your reference to viewController. From what you mentioned about the compilation error, it would seem that you do not have this relationship setup. As it happens, you might not need this anyway as you can essentially achieve the same thing by setting the rootViewController property of your NSWindow.

Again, I would recommend that you read up on some beginner tutorials for iOS development or purchase a decent book as issues like this are fairly fundamental. If you're having troubles with this alone, you will only find yourself more confused and likely to require assistance one you start delving into more complex code.

Zack Brown
  • 5,990
  • 2
  • 41
  • 54
0

I have the solution in Swift and have published an example on GitHub to share:

https://github.com/AppLogics/DynamicEntryPointExample-iOS

I will explain here with a simplified version of it to dynamically/programmatically set the Entry Point from between a blue and a red View Controller.

Ensure in interface builder you remove all entry points from your storyboard, in order to do this you will need to select the ViewController with the Entry Point and uncheck the Is Initial View Controller option under the View Controller section:

enter image description here

You then need to ensure all potential entry points are assigned Storyboard Id's as you will use this in code from the AppDelegate.swift file. This example uses blue and red

enter image description here

Next you must edit the AppDelegate.swift file. First declare the constant to test against and decide which View Controller to display initially, in this case:

let blueEntryPoint = true

Next insert some code in the default didFinishLaunchingWithOptions function between the comment: // Override point for customization after application launch. and the statement: return true like so:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //checking to see if the blue is true or false
    if blueEntryPoint {
        //Showing blue View Controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mainViewController = storyboard.instantiateViewController(withIdentifier: "blue")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = mainViewController
        self.window?.makeKeyAndVisible()
    } else {
        //Not showing blue, i.e. will show red View Controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let surveyViewController = storyboard.instantiateViewController(withIdentifier: "red")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = surveyViewController
        self.window?.makeKeyAndVisible()
    }

    return true
}

Run this, then change the blueEntryPoint to false and re-run it!

SagarScript
  • 1,145
  • 11
  • 15