Strictly Programming

Adding a login window to mac os x app

Posted in Objective-C, OS X by sqllyw on 09/29/2013

Adding a login window into a non document based os x app is quite simple,.

you can use a sheet as a login window in applicationDidFinishLaunching, the main windows ‘visible during launch’ should be set to NO in the Interface Builder, after getting the login info, you can set the ‘visible during launch’ to YES if info is correct, or terminate the app with [[NSApplication sharedApplication] terminate:self]

#1

Add a NSWindow to the xib, set ‘visible during launch’ to NO, set that too for the main window.

#2


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // prompt for password
    [NSApp beginSheet:loginSheet modalForWindow:NULL modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
}

// terminate if password invalid

- (IBAction)endLoginSheet:(id)sender
{
    [NSApp endSheet:loginSheet];
    [loginSheet orderOut:sender];
    NSString *pwd = [_password stringValue];
    
    if ([pwd isEqualToString:@"password"])
        [_window setIsVisible:YES];
    else
        [[NSApplication sharedApplication] terminate:self];
}


Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: