views:

355

answers:

2

I am working on an iPhone application. When the application starts I authenticate, establish a Restful connection, get data in form of XML file, parse it and then display it in form of table view. Here is my problem. If the user information is already stored on the application or if it is hard-coded in the code, the application takes the authentication user name and password and proceeds to display data in table format. I was till now testing with hard-coded data. Now I want to display another view which asks for user name and password if it is not present in the application..i.e. I want to make the user name and password dynamic as opposed to static. In my case when the use is busy inputting the user name and password, the "viewDidLoad" method tries to establish a connection with the server with blank user name and password failing the server connection and the table loading. But if I run the application the second time, the application finds the user name and password entered previously and loads the tables fine.

Is there a way I can delay the process of server connection if the application does not find the user name and password? How can I signal the server connection method after the user has entered his user name and password?

Thanks for your help.

A: 

Once you display a view in a window ViewDidLoad will ALWAYS be called, thats why its called viewDidLoad...What you need to do, is create a diffrent view for the login steps, then when the user is done loging in they will click a login button which will push the view that connects to your restful service. If the user data i s already present, then skip the login page all together and go straight to your table view view

Daniel
You can also ofcourse not have all the code that does the table loading in viewDidLoad, instead have another method that is called when you need it to be
Daniel
+1  A: 

Can you post some of your code? It's difficult to speculate without seeing what's going on but I'll give it a try anyway.

You need to move the code where you connect to the server out of viewDidLoad and put it in a callback method or something. I would suggest throwing an alert with a name and password field, make your view controller the alert's delegate and then have the clickedButtonAtIndex function connect to the server.

Example:

-(void)viewDidLoad {
    UIAlertView *loginAlert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Please enter your username and password below" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login",nil];

    //You might have to do some fiddling with the frame sizes to make it fit in the alert.
    UITextField *userNameField = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, lengthX, lengthY)];
    userNameField.placeholder = @"User Name";
    [loginAlert addSubview:userNameField];
    //loginAlert will retain this so we release it now.
    [userNameField release];



    [UITextField *passwordNameField = [[UITextField alloc] initWithFrame:CGRectMake(originX, originY, lengthX, lengthY)];
    passwordNameField.secureTextEntry = YES;
    [loginAlert addSubview:passwordNameField];
    [passwordNameField release]

    //This line tells the iPhone to stretch out your AlertView so it's big enough for your two textViews, we use 0 and 130 respectively but we only have one text field.
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(stretchX, stretchY);

    [loginAlert setTransform:myTransform];
    [loginAlert show];
    [loginAlert release];
}


//Implement this method, it will be called when the user clicks the "login" button on your alert.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //Usually if they click cancel you do nothing, thats buttonIndex 0
        if(buttonIndex == 1) {
     //Connect to the server with the correct login and password.
    }
}
Tim Bowen
Hello Tim, Thanks for your suggestion. By inputting the user name and password by using an alert has solved my issue. I was inputting the user name and password in a second view, so I was having an issue of reloading the table views in the root view when the Login button was clicked in the second view. My root view is a complex one as it involves a tab controller, a navigation controller, table view and a 3 button segmented control. So, I was finding it hard to update the root view tables when the login button was clicked in the subview. Thanks for your suggestion, it works great now.
Happy to be of assistance!
Tim Bowen
I'm pretty sure adding a UITextField to a UIAlert is against Apple's HIG. If you plan to submit this software to the App Store, you might be safer showing a modal view instead of a UIAlert (using -presentModalViewController:animated:).
leolobato
That's a good point. Here's a [link to the relevant section of the HIG][1]. I have an app in the store that uses this method to get a user's email address, I guess I got lucky. Most of the applications that I use that require a user name and password pop up a floating box in the middle of the screen. I don't think any humans will be offended by this interface but it is a concern. [1]: http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/mobilehig/ModalViews/ModalViews.html#//apple_ref/doc/uid/TP40006556-CH11-SW1
Tim Bowen