Question

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:password@ftp.example.com/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.

Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top