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 *)a
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.
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.
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:
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
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!