Show UIViewController only the first time the app is opened


I have a problem, I have implemented GCM in my application and I have created a UIViewController to be the initial one and in which the necessary registration of this system is done.

Once done, jump a UIAlertController whose button takes you directly to the initial page of the application through a segue manual. So far no problem but I want to know how to do so from then on, the segue is made directly without showing this ViewController .

Thank you very much.

asked by Javi Rando 28.02.2016 в 16:50

1 answer


I think it's a concept error. That is, make your application go directly to UIViewController that will appear after login, and present this screen in the viewWillAppear of the original VC. Then, when you jump%% of% you just have to do UIAlertController of the login VC.

Something like this should work:

override func viewWillAppear(animated: Bool) {
    let defaults = NSUserDefaults.standardUserDefaults()
    if ((defaults.objectForKey("firstRun") == nil)) {
        defaults.setObject(NSDate(), forKey: "firstRun")

        let loginVC = UIViewController()
        self.presentViewController(loginVC, animated: true, completion: nil)

In Objective C:

- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults objectForKey:@"firstRun"]) {
        [defaults setObject:[NSDate date] forKey:@"firstRun"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        UIViewController *loginVC = [[UIViewController alloc] init];
       [self presentViewController:loginVC animated:YES completion:nil];

answered by 29.02.2016 / 10:35