1

I am trying create a login screen which sends login info sharepoint server and i expect to be able to successfully login.

There are plenty of old examples and libraries which I am not able to use. But after spending hours I found this link to have the crux of all

http://transoceanic.blogspot.com/2011/10/objective-c-basic-http-authorization.html

My code looks like this now:

- (void) startLogin {

NSURL *url = [NSURL URLWithString:@"http://site-url.com"];

NSString *loginString =(NSMutableString*)[NSString stringWithFormat:@"%@:%@",usernameTextField.text,passwordTextField.text];

NSData *encodedLoginData=[loginString dataUsingEncoding:NSASCIIStringEncoding];

NSString *authHeader=[NSString stringWithFormat:@"Basic %@",  [encodedLoginData base64Encoding]];

NSURLRequest *request = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:3.0];


//    [request setValue:authHeader forKey:@"Authorization"];

[request setValue:authHeader forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"POST"];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

There are three issues:

  • the commented out line-code doesn't give any error but it crashes on that line(while debugging)

  • on [request setValue:authHeader forHTTPHeaderField:@"Authorization"]; i am getting error "No visible interface for NSURLRequest declares selector setHTTPHeaderField"

  • Also, I am getting warning - unused variable "connection" in last line. I am not sure how this whole thing works and any simple example or correction is appreciated.

I would also like to know if there are any other simple methods for basic auth.

UPDATE: Delegate methods

  - (void)connection:(NSURLConnection *)connection
 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    // Access has failed two times...
if ([challenge previousFailureCount] > 1)
{

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Error"
                                                    message:@"Too many unsuccessul login attempts."
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
}
else
{
    // Answer the challenge
    NSURLCredential *cred = [[NSURLCredential alloc] initWithUser:@"admin" password:@"password"
                                                       persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
  }
 }


 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSLog(@"Connection success.");

 }

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
  NSLog(@"Connection failure.");

 }
Obj-Swift
  • 2,802
  • 3
  • 29
  • 50

1 Answers1

2

Change NSURLRequest to NSMutableURLRequest to access it's setValue:forHTTPHeaderField method, and add a HOST header as well if it's a shared web host.

At the end, you have to start the connection:

[connection start];

Also, make sure you've set up your NSURLConnectionDelegate delegate methods for the call backs.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • Great. That worked. Catch is it directly goes to the delegate method - (void)connectionDidFinishLoading:(NSURLConnection *)connection. So I am getting success message no matter what. – Obj-Swift Aug 01 '13 at 19:40
  • @akash, that means the web server responded. Check the HTTP response code (`NSHTTPURLResponse.statusCode` ) in the `didReceiveResponse` delegate method. – Marcus Adams Aug 01 '13 at 19:48
  • @akash, try accessing something you **do** have access to. :) – Marcus Adams Aug 01 '13 at 20:05
  • lol. I do have access to this but I must be doing something wrong. I think i'll have to spend some more time on this.anyway, thanks for your help :) – Obj-Swift Aug 01 '13 at 20:11
  • Instead of putting the basic auth in the request, i implement: -connection:didReceiveAuthenticationChallenge: via the NSURLConnectionDelegate Protocol – MobileGuy Aug 02 '13 at 11:50
  • @MobileGuy Thanks. I tried that but somehow connection:didReceiveAuthenticationChallenge methods is not getting called. here;s the how it looks like -> https://gist.github.com/akashtrivedi/e1df0b88a9c61ffbf158. – Obj-Swift Aug 02 '13 at 19:36