Iphone UIWebView HTTP Basic Authentication

So I need to send a user to a page that requires a login. I want to save those details in the app and send them with the initial request. But HOW?

#import "NSData+Additions.h"

@implementation Utility

+ (void) addAuthToWebRequest:(NSMutableURLRequest*)requestObj email:(NSString*)email password:(NSString*)password{
NSString *authString = [[[NSString stringWithFormat:@"%@:%@", email, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];

authString = [NSString stringWithFormat: @"Basic %@", authString];

[requestObj setValue:authString forHTTPHeaderField:@"Authorization"];
}

Which uses the base64 encoding provided by the Objective Resource library.

Then,

NSURL *url = [NSURL URLWithString:urlAddress];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];

[Utility addAuthToWebRequest:requestObj email:[Account getCurrentAccount].email password:[Account getCurrentAccount].password];

GREAT!

[edit]from Jacques Lema’s comment[/edit]

Posted on February 20, 2009 at 11:14 am by Jordan Carter · Permalink
In: Iphone · Tagged with: , ,

8 Responses

  1. Written by Jacques Lema
    on February 22, 2009 at 11:07 pm
    Permalink

    I have been trying to do this for hours but I always get this error:

    Error Domain=NSURLErrorDomain Code=-999 UserInfo=0x5905e0 “Operation could not be completed. (NSURLErrorDomain error -999.)”

    It works fine with other non-auth URLs of course (it’s a new project with pretty much nothing in it than two tabs with two web views).

  2. Written by Jacques Lema
    on February 22, 2009 at 11:23 pm
    Permalink

    Actually the NSURLErrorDomain only appears because the previous request is being cancelled each time I click again.

    It doesn’t happen for the first request. It appears however that this first request lasts forever..

    Do you do just call that aftewards?:
    [webView loadRequest:request];

    Or did I miss something in between?

  3. Written by Jacques Lema
    on February 23, 2009 at 12:34 am
    Permalink

    Ok, that’ll teach me about copy pasting without reading :-)

    This works:

    NSString *authString = [[[NSString stringWithFormat:@”%@:%@”, user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
    authString = [NSString stringWithFormat: @”Basic %@”, authString];
    [requestObj setValue:authString forHTTPHeaderField:@”Authorization”];

    Your version generated a header like this:
    Authorization: basic: XXX=

    While the correct header should be (as sent by Safari):
    Authorization: Basic XXX=

    I had to use packet peeper to spot the difference.

  4. Written by Jordan
    on February 23, 2009 at 10:02 am
    Permalink

    Jacques Lema :

    Ok, that’ll teach me about copy pasting without reading :-)

    This works:

    NSString *authString = [[[NSString stringWithFormat:@”%@:%@”, user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
    authString = [NSString stringWithFormat: @”Basic %@”, authString];
    [requestObj setValue:authString forHTTPHeaderField:@”Authorization”];

    Your version generated a header like this:
    Authorization: basic: XXX=

    While the correct header should be (as sent by Safari):
    Authorization: Basic XXX=

    I had to use packet peeper to spot the difference.

    My version has a capital B, I dont see the difference. It is possible it is not correct, as I have seen it done differently. But this works for my rails app.

  5. Written by Jacques Lema
    on February 26, 2009 at 10:25 am
    Permalink

    It’s not about the capital B. It’s about the second semi-colon after basic:. It sure didn’t work for me running apache.

  6. Written by admin
    on February 26, 2009 at 10:41 am
    Permalink

    Yeah, I have seen that problem somewhere else on the net, I have updated in response. Cheers!

    Not sure why mine isn’t affected.

  7. Written by Naresh
    on May 7, 2009 at 9:07 am
    Permalink

    Hi,

    i am trying to access a page from IIS which is using Windows Authentication. I could not able to browse the wep page. can you help me in this..

    Thanks

  8. Written by Jordan
    on May 12, 2009 at 12:32 pm
    Permalink

    Naresh :

    Hi,

    i am trying to access a page from IIS which is using Windows Authentication. I could not able to browse the wep page. can you help me in this..

    Thanks

    Sorry but I don’t have any experience in Windows Auth. I’m pretty much all opensource dev here. Apart from iPhone obviously.

    If there is an option for the Windows Auth to use “Basic Authintication” that might be where to continue looking. Basic Auth isn’t very secure, if at all, so Windows Auth might not let you.

Leave a Reply