Tuesday, 31 January 2012

Customized UIAlertView to Input Credential

Hi All,

I Found lot of UIAlertView customization but I cannot able to find a nice one to take credential input, I need such alert view for an NTLM authentication for a web service I called in my recent project. It is mainly due to in browser itself NTLM credential is input using a alert popup and my customer also requires it.
so I search over google and I got lot of samples but no one matches some of my requirement so I write my own customized UIAlertView with following specialities,

1. It support multiple line for title and message and it will 'word' wrapped the text.
2. As in case of ordinary UIAlertView if message is too big it will add to a UITextView instead of a label, I overcome it.
3. Set the frame of AlertView as per the text length.
4. Label size is align with Text (Word wrapping of text with correct alignment to top left).
5. Support for iOS3.2 and above including iOS5.0.
6. Handle proper display and dismiss of keyboard with Next action.
7. Support for iPhone and iPad with orientation.

See the following Screen Shots,

//To Display the Customized UIAlertview named as NSAuthAlertView
(Here prefix NS means not 'Next Step' it just 'Naveen Shan' my name, if you don't like change it.)

1. Paste this function in the class you wish to call,

 + (void)showCredentialInputAlert:(NSString *)alertTitle message:(NSString *)strAlertMessage delegateObject:(id)delegate viewTag:(int)iTag  {  
   NSAuthAlertView *alertView;  
   @try {  
     alertView=[[NSAuthAlertView alloc] initWithTitle:alertTitle message:strAlertMessage delegate:delegate viewTag:iTag];  
     [alertView show];  
     [alertView release];  
     alertView  =  nil;  
   @catch (NSException * exception) {  
     NSLog(@"Exception on showCredentialInputAlert : %@",[exception description]);  
   @finally {  
     [alertView release];  
     alertView  =  nil;  

2. call the above method like as follows,

 [[self class] showCredentialInputAlert:@"NSAuthAlertView" message:[NSString stringWithFormat:@"Enter Credentials for Login"] delegateObject:self   

3. As a best practice you need to define tag value,

//To catch the response (Username and Password) form NSAuthAlertView

1. Paste the following alert view delegate,

 #pragma mark - UIAlertView delegate  
 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
   if (alertView.tag == KAUTHENTICATEALERTTAG) {  
     if (buttonIndex == 1)  {  
       NSString *username = ((UITextField *)[alertView viewWithTag:(KAUTHENTICATEALERTTAG + 1)]).text;  
       NSString *password = ((UITextField *)[alertView viewWithTag:(KAUTHENTICATEALERTTAG + 2)]).text;  
       if (username && password)  {  
         //validation of credential is done here.  
         //[self validCredential];  
       else     {  
         //empty credential.  
     else if (buttonIndex == 0) {  

I attached complete code here

if you need more details feel free to contact me

Naveen Shan

No comments:

Post a Comment