Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I just added a password protect to a folder on my website. My current project accesses a folder in the secured folder which obviously can't get to now. Can someone point me to any Doc's or info about sending the username and password that I can embed into a method. Here is my Method so far.

Code:
-(void)foodSection{
    int location = 0;
    if (nameArray == nil) {
        nameArray = [[NSMutableArray alloc] init];
        myURL = [NSURL URLWithString: @"http://www.sbtraveler.tv/ios/test/name.txt"];
        bundeledInformation = [[NSString alloc] initWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:nil];
        
        for (int i = 0; i < 4; i++) {
            
            NSScanner *scanner = [NSScanner scannerWithString:bundeledInformation];
            [scanner setScanLocation: location];
            
            
            [scanner scanUpToString:@"\n" intoString:&tempString];
            location = [scanner scanLocation];
            [nameArray addObject:tempString];
            NSLog(@"Item: %@", [nameArray objectAtIndex:i]);
        }
    }
           
    else{
        NSLog(@"Already an Array");
    }
    [testLable setText:bundeledInformation];
    [testLable setHidden:NO];
    
    
}

Thanks
 
Do you need a cookie issued by the server to access the password protected content? Or can you just send the user/password with the request as parameters?

-Lee
 
You can do it programmatically, but you can also do it quick and hacky:

Code:
 myURL = [NSURL URLWithString: @"http://USERNAME:PASSWORD@www.sbtraveler.tv/ios/test/name.txt"];

The stack overflow link is the way to do it programmatically.

I've done it both ways.
 
Last edited:
Thanks for the links, I will read through them and see if I can get it to work.

-Lars
 
haha..The hacky way totally worked!

I am trying to understand how that code works?
Code:
myURL = [NSURL URLWithString: @"http://USERNAME:PASSWORD@www.sbtraveler.tv/ios/test/name.txt"];

When I normally go to that page a prompt pops up with the USERNAME and the text box is enabled. Then I need to tab or click in the PASSWORD box to enter the pass.

So the simple code USERNAME:pASSWORD uses the : to tab to the next field?

I am so baffled as to how this simply code works.
 
You're thinking of this as if the code is acting through a browser interacting with a user. Normally you ask for a resource on a server with a URL. If the resource requires a password the server will respond to the browser with a 403 error, and say that a username and password can be sent. Your browser then shows the user/password box based on this response. Once you enter this, the request is made again with the Authentication attribute set to an encoded version of your username and password. If this is accepted the server sends the requested content, if not it send another error. You can skip the initial request and error response and just send a request with the authentication included initially. Adding the username:password instructs the http client to include this info in your request.

-Lee
 
I see Lee, Thanks you for the detailed explanation. There is a lot happening behind the scenes that I don't see. It just seemed so easy and all with in 1 line of code. I am sure there is additional code I can / should enter for error catching in case it can't find a folder or there is no internet connection.

Thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.