In this application we will see how to display login screen only first lunch after downloading the application in iPhone. So let see how it will worked.
Step 1: Open the Xcode, Create a new project using Window Base application. Give the application “TabBarWithLogin”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Expand Resources and notice the template generated a separate nib, MainWindow.xib for the TabBarWithLogin application.
Step 4: We need to add one UIViewController class in the application. So select NewFile -> UIViewController subclass. And give the class name “MainView”.
Step 5: Open the TabBarWithLoginViewcontroller.h file and make the following changes.
@class MainView;
@interface TabBarWithLoginAppDelegate : NSObject <UIApplicationDelegate,UITextFieldDelegate> {
IBOutlet UITextField *nameInput;
IBOutlet UITextField *passInput;
IBOutlet UILabel *greeting;
NSMutableData *webData;
MainView *mainView;
UIView *view;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIView *view;
@property (nonatomic) bool showUsageAlert;
@property(nonatomic, retain) IBOutlet UITextField *nameInput;
@property(nonatomic, retain) IBOutlet UITextField *passInput;
@property(nonatomic, retain) IBOutlet UILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;
@end
Step 6: In the TabBarWithLoginViewcontroller.m file make the following changes:
#import "MainView.h"
@implementation TabBarWithLoginAppDelegate
@synthesize window=_window ,showUsageAlert,nameInput,passInput,greeting,webData,view;
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL ranBefore = [defaults boolForKey: @"ranBefore"];
[defaults setBool: YES forKey:@"ranBefore"];
[defaults synchronize]; // save to disk
if (ranBefore){
NSArray *documentPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
mainView = [[MainView alloc]
initWithNibName:@"MainView"
bundle:nil];
[view removeFromSuperview];
[self.window addSubview:mainView.view];
[mainView displayScreen];
}
else{
if(showUsageAlert = YES){
[self performSelector:@selector(showUsageAlertDialog) withObject:nil afterDelay:0.0];
}
}
}
– (void)showUsageAlertDialog
{
if (showUsageAlert=YES) {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.window addSubview:view];
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 100)];
myLabel2.text = @"User Name";
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
myLabel1.text = @"Password";
myLabel1.backgroundColor = [UIColor clearColor];
[view addSubview:myLabel2];
[view addSubview:myLabel1];
nameInput = [[UITextField alloc] initWithFrame:CGRectMake(100, 70, 150, 31)];
nameInput.borderStyle = UITextBorderStyleRoundedRect;
nameInput.textColor = [UIColor blackColor];
nameInput.font = [UIFont systemFontOfSize:17.0];
nameInput.placeholder = @"Enter User name"; //place holder
nameInput.backgroundColor = [UIColor clearColor];
nameInput.autocorrectionType = UITextAutocorrectionTypeNo;
nameInput.backgroundColor = [UIColor clearColor];
nameInput.keyboardType = UIKeyboardTypeDefault;
nameInput.returnKeyType = UIReturnKeyDone;
nameInput.clearButtonMode = UITextFieldViewModeWhileEditing;
[view addSubview:nameInput];
nameInput.delegate = self;
passInput = [[UITextField alloc] initWithFrame:CGRectMake(100, 130, 150, 31)];
passInput.borderStyle = UITextBorderStyleRoundedRect;
passInput.textColor = [UIColor blackColor];
passInput.font = [UIFont systemFontOfSize:17.0];
passInput.placeholder = @"Enter Password"; //place holder
passInput.backgroundColor = [UIColor clearColor];
passInput.autocorrectionType = UITextAutocorrectionTypeNo;
passInput.backgroundColor = [UIColor clearColor];
passInput.keyboardType = UIKeyboardTypeDefault;
passInput.returnKeyType = UIReturnKeyDone;
passInput.clearButtonMode = UITextFieldViewModeWhileEditing;
[view addSubview:passInput];
passInput.delegate = self;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(20, 200, 97, 37);
[view addSubview:button];
}
}
–(IBAction)buttonPressed:(id)sender
{
mainView = [[MainView alloc]
initWithNibName:@"MainView"
bundle:nil];
[view removeFromSuperview];
[self.window addSubview:mainView.view];
[mainView displayScreen];
}
– (void)applicationWillResignActive:(UIApplication *)application
{
}
– (void)applicationDidEnterBackground:(UIApplication *)application
{
}
– (void)applicationWillEnterForeground:(UIApplication *)application
{
}
– (void)applicationDidBecomeActive:(UIApplication *)application
{
}
– (void)applicationWillTerminate:(UIApplication *)application
{
}
– (void)dealloc
{
[_window release];
[super dealloc];
}
@end
Step 7: Open the MainView.h file and make the following changes:
@interface MainView : UIViewController {
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
–(void)displayScreen;
@end
Step 8: Double click the MainView.xib file and open it to the view window. Drag the TabBar Controller from the library and place it to the Interface Builder. Connect File’s owner icon to the TabBar Controller and tabBarController. Now save the .sib file, close it and go back to the Xcode.
Step 9: Open the MainView.m and make the following changes:
@implementation MainView
@synthesize tabBarController;
–(void)displayScreen
{
[self.view addSubview:tabBarController.view];
}
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
– (void)dealloc
{
[super dealloc];
}
– (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
#pragma mark – View lifecycle
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
– (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 10: Now Compile and run the Application on the Simulator.
You can Download SourceCode from here TabBarWithLogin
