Show a View on First Launch Only - Swift 3
Probably, you mean "Show a ViewController on First Launch Only".
Using UserDefaults for this purpose is a good idea, however, checking if the term accepted should not be at the TermsAndConditionsViewController
layer, instead, it should be in AppDelegate - application:didFinishLaunchingWithOptions
, you can decide in it whether the root ViewController should be the TermsAndConditionsViewController
or the other ViewController (HomeViewController for example).
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(withIdentifier: UserDefaults.standard.bool(forKey: "termsAccepted") ? "termsViewControllerID" : "homeViewControllerID")
window?.rootViewController = rootViewController
return true
}
TermsAndConditionsViewController:
class TermsAndConditionsViewController: UIViewController {
//...
@IBAction func acceptButtonTapped(_ sender: Any) {
UserDefaults.standard.set(true, forKey: "termsAccepted")
performSegue(withIdentifier: "toPeekView", sender: sender)
}
//...
}
Hope this helped.
How can I show a view on the first launch only?
You put in in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//first-time ever defaults check and set
if([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]!=YES)
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"TermsAccepted"];
}
Then you implement in your rootViewController the terms and conditions and a way to accept it.
You will have to check if the terms are accepted, for example like this:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
//proceed with app normally
}
else{
//show terms
}
When accepted, the following code will change the default settings:
if(termsaccepted){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"TermsAccepted"];
}
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()
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")
}
Show screen on first launch only in iOS
In your viewDidLoad:
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:@"aValue"]]) {
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"aValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Action here
}
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
How can I display page controller only on the first launch IOS Swift
You can use UserDefaults
to set a Bool
value which defines if is the first time the App is opened.
So before present the page you ask for the value in UserDefaults
, If the value doesn't exists you set it.
UserDefaults.standard.set( true, forKey: "firstTimeOpened")
EDITED
I suppose your Home View Controller
is FirstVC
so try this:
In your FirstVC
on the ViewDidAppear
function:
let firstTime = UserDefaults.standard().object(ForKey: "first_time") as? Bool // Here you look if the Bool value exists, if not it means that is the first time the app is opened
// Show the intro collectionView
if firstTime == nil {
let view = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourPageVC") // Instatiates your pageView
show(view, sender: nil)
UserDefaults.standard().set(false, forKey: "first_time")
}
After that you can dismiss your PageView
and return to home, if you restart your app the PageView will not appear.
Related Topics
Uicollectionview Auto Scroll to Cell at Indexpath
Show iPhone Cut Copy Paste Menu on Uilabel
-[Nsnull Length]: Unrecognized Selector Sent to JSON Objects
How to Programmatically Add a Proxy to an Nsurlsession
iOS 7 - How to Disable the Swipe Back and Forward Functionality in Safari
Getting Text from Image on iOS (Image Processing)
Warning: Attempt to Present * on * Whose View Is Not in the Window Hierarchy - Swift
Swift Merge Audio and Video Files into One Video
How to Call JavaScript Function in Objective C
Storyboards VS. the Old Xib Way
How to Change the Default Text of Cancel Button Which Appears in the Uisearchbar +Iphone
Z-Index Issue on iOS Safari & Chrome
Uicollectionview Update a Single Cell