user1498477
user1498477

Reputation:

How to create a modal view , for application launch in iOS?

How could i create a modal view , maybe from the delegate to check if its the first time the application launches and show a modal view with a dismiss button , in order to inform the user about something important?

I need the view to show ONLY the first time the application is launched and never again.

Upvotes: 0

Views: 1457

Answers (4)

Dilip Manek
Dilip Manek

Reputation: 9143

You can achive this using NSUserDefaults.

In delegate.h file

NSString * firstTime;

In .m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *myStr = [defaults objectForKey:@"str"];
    firstTime =myStr;

    if (firstTime.length==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hi" message:@"You have open this app first time" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
        [alert show];
        [self firstTime];
    }

    return YES;
}

-(void)firstTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *myStr = @"first";
    [defaults setObject:myStr forKey:@"str"];
    [defaults synchronize];
}

Upvotes: 1

Patrick
Patrick

Reputation: 7185

Firstly: Check out an earlier answer of mine here: https://stackoverflow.com/a/13563490/1359306

This is for a password protection modal view which I use every time the app is opened. The answer should help a few issues that may arise when implementing your solution. It uses performSegueWithIdentifier as I am using storyboards, but you could also use presentViewController:animated:completion: to present you view.

Then: Note that there is an if statement in place which you can use check if you need to present the view or not. In my case I check to see if the user has been out of the app for more than 5 minutes. I do this by setting a date in NSUserDefaults each time applicationWillResignActive is called - and checking the difference between that and the current date/time).

In your case you could do something like the following:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"firsttime"] == nil) {
    //display modal view
    [defaults setObject:@"YES" forKey:@"firsttime"];
    [defaults synchronize];
}

This checks the NSUserDefaults of the app. If 'firsttime' is nil (which it will be when app is first downloaded) we will show the view. We then set the key to "YES" which means it will never equal nil again - so the view will never show.

This is useful for showing instructions when the app first loads. Alternatively, you could store dates or numbers to make the code more adaptable in the future.

The docs for NSUserDefaults can be found here.

Upvotes: 3

nsgulliver
nsgulliver

Reputation: 12671

in your AppDelegate using method didFinishLaunchingWithOptions check if your application is launching first time or 2nd time like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstTime"])
    {
        // show your main view controller
    }
    else
    { // your app is launching first time ....

      // show your modalview controller here and dismiss it & go to main view controller


      // don't forget to update the key so that next time user don't hang in else statement...

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstTime"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
}

Upvotes: 1

David Hope
David Hope

Reputation: 2256

You can use the presentViewController:animated:completion: in the UIViewController class.

So here is what I'd do:

  1. Load your main view controller
  2. Check your user preferences to see if your "firsttime" key exists (in your view did load most likely)
  3. If it doesn't display your one-time view
  4. When it completes (which you can know by having a delegate call back to your main view controller), save the "firsttime" key to your user preferences

no source, code (my Mac is at home), but if you follow these simple steps it should work fine for you.

Upvotes: 0

Related Questions