How to present a different view controller only at first launch of iOS app (Swift) - programmatically
You can try to save that state in UserDefaults
if !UserDefaults.standard.bool(forKey: "LaunchedBefore") {
window!.rootViewController = LaunchViewController()
UserDefaults.standard.set(true, forKey: "LaunchedBefore")
} else {
window!.rootViewController = ViewController()
}
window!.makeKeyAndVisible()
How can I change initial view controller after first launch?
after your app lauch, it will call the didFinishLaunchingWithOptions method in the app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// here you have the chance to change your rootview controller
if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
UserDefaults.standard.set(true, forKey: "notFirstInApp")
window?.rootViewController = your tutorial view controller
}else{
window?.rootViewController = your main viewcontroller
}
return true
}
Have ViewController appear only on first launch
There is no way to tell if it's the first launch built into the UIKit framework, however you can do it another way.
In AppDelegate.swift, go to applicationDidFinishLaunchingWithOptions method (the first one, usually). Now, add this:
//Check for key "first_launch" in User Defaults
if let _ = NSUserDefaults.standardUserDefaults().objectForKey("first_launch") {
//Set your own global variable to be true. Then, when your ViewController
//loads, do a popup window with that DoctorViewController thingy if the
//variable is true
//Example:
isFirstLaunch = true
//Then, set "first_launch" to be a value so your app will never call this block again
NSUserDefaults.standardUserDefaults().setObject("", forKey: "first_launch")
}
Creating a first launch viewcontroller
Create a new ViewController. Import the header in appDelegate.h
file also create a instance variable of that class with name initialViewController
.
Change your else condition like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
NSLog(@"not first launch");
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.initialViewController = [[InitialViewController alloc] initWithNibName:@"InitialViewController" bundle:nil];
self.window.rootViewController = self.InitialViewController;
NSLog(@"first launch");
}
[self.window makeKeyAndVisible];
return YES;
}
Create a ViewController that shows on First Launch Only
I think you can do something like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
BOOL userHasOnboarded = [[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstTime"];
if (userHasOnboarded) {
[self setupNormalRootViewControllerAnimated:YES];
}
else {
self.window.rootViewController = [self PushExplanation];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)setupNormalRootViewControllerAnimated:(BOOL)animated {
UIViewController *mainVC = [UIViewController new];
mainVC.title = @"Home View Controller";
if (animated) {
[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainVC];
} completion:nil];
}
else {
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainVC];
}
}
In your PushExplanation method you can show the UIviewController you want to show once and then in the same UIViewController you can ask for Push Notifications like this
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Hope this help you
Creating ViewController to only run first time the application is run iOS
I would go with your approach of presenting a modal on top of your root view controller when you detect first launch.
Use presentViewController:animated:completion:
to display it, with animated: set to NO. That way it will appear on top of the root view controller and cover it. Then when you're done with the first view controller you can dismiss it and reveal your root view controller underneath.
Change initial view controller after first launch of app
For one of my apps, where I did similar to this, I have my "main" view controller check on viewDidLoad for a setting in the default settings that would indicate if the user had signed in. If they hadn't, I immediately pushed the loginViewController without animation and the user filled in the appropriate forms. When that was dismissed, I reloaded the view in my main view controller.
My client liked the app and it looked nice.
Related Topics
How to Use Pod in Notification Service Extension
Reusing View in UIpickerview with iOS 7
Notification in Swift Every Day at a Set Time
Data Repeat in UItableview When Scrolling
iOS - Emphasise with Bold Strings in Localizable.Strings
Stick UItableview Header View to Top When Creating Header in Storyboard
Remove Duplicate Structs in Array Based on Struct Property in Swift
Avplayer Removing Observer Crash in Swift 2.2
Nstextattachment Image Not Shown in Nstextview (But in Uitextview)
Segue from Uitableviewcell by Tapping on an Image Inside of Cell
Google Signin: Unable to Disconnect iOS App
iOS - Combine/Concatenate Multiple Audio Files
Track Touch Points in Multitouch
Insert a Floating Action Button on Uitableview in Swift
Force UIsplitviewcontroller to Always Show Master (Only) in Landscape (On iPhone 6 Plus)