0

NSURL接続を使用しているため、FTP接続からファイルをダウンロードしたい。接続にはユーザー名とパスワードがあります。

URLにユーザー名とパスワードを渡すと

ftp://user:password@ftp.example.com/foo/bar.zip

それは正常に動作します。しかし、コールバックでパスワードを渡すことができる認証方法を実装したいと考えています。しかし、私はコールバックを受け取っていません。

以下は私が実装したコールバックですが、呼び出されることはありません

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

注:- SimpleFTPSample という Apple サンプルを使用してみましたが、役に立ちませんでした。

4

1 に答える 1

0

コールバックを受信するには、次の両方を確認してください。

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

そして、メイン キューから呼び出す必要があります。

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

次に、u コールバックが実行されます。u 認証部分のユーザー名とパス トリックを使用すると、コードから解析できなくなります。

- (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];
}

しかし、私が知っているように、ユーザーとパスは保護なしで送信されるため、コンテンツを https サーバーに移動することをお勧めします。その場合、あなたはそれを確信することができます

于 2012-03-04T20:02:30.977 に答える