accessign my seured web folder.

Discussion in 'Mac Programming' started by larswik, Sep 3, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. larswik, Sep 3, 2011
    Last edited: Sep 3, 2011

    larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
  4. Ap0ks macrumors 6502

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #4
  5. ulbador, Sep 4, 2011
    Last edited: Sep 4, 2011

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #5
    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.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    Thanks for the links, I will read through them and see if I can get it to work.

    -Lars
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    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.
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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
     
  9. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #9
    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.
     

Share This Page