Search code examples
macoscocoaftpftp-client

cocoa FTP connection authentication concern


I want to download a file from FTP connection for that I am using NSURL connection. The connection have username and password.

If I pass the username and password in the url

ftp://user:[email protected]/foo/bar.zip

than it works fine. but I want to implement the authentication method where I can pass the password in the callback. But I am not receving any callbacks.

Below is the callback that I have implemented but it never gets called

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

Note:- I tried using Apple sample called SimpleFTPSample but it didnt helped.


Solution

  • To receive callbacks, please check both things:

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestToServer delegate:self startImmediately:YES];
                                                                                                     ^^^^^^ must be
    

    And u must call it from main queue:

    dispatch_async(dispatch_get_main_queue(), ^(void) { // u url part here
    

    Then, u callbacks will be done, u can using authenticate part user name and pass trick can impossible to parse then from code:

    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    
        NSString *user = [NSString stringWithFormat:@"%c%s%@", 'u', "se", @"r"];
        NSString *password = [NSString stringWithFormat:@"%c%s%c%@", 'B', "FEBB", 'C', @"3CD036ED072A"];
    
        NSURLCredential *credential = [NSURLCredential credentialWithUser:user
                                                                 password:password
                                                              persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }
    

    But as i know, user and pass will be sent anyway without any protection, so better solution is moving u content to https server. In that case u can be sure that u