Iphone 未使用ftp协议调用didReceiveAuthenticationChallenge

Iphone 未使用ftp协议调用didReceiveAuthenticationChallenge,iphone,xcode,ftp,nsurlconnection,Iphone,Xcode,Ftp,Nsurlconnection,我已经为我的应用程序中的NSURLConnection问题挣扎了几天。我需要使用ftp协议连接到服务器(调用getPdfFromServerAtIndex),以获取对象(pdf页面,但这并不重要!)。当我尝试连接到http url时,我没有问题。对于请求身份验证的http url,所有委托方法都会被调用 但是,当我使用下面显示的url(使用ftp协议构建)时,像didReceiveAuthenticationChallenge、CanAuthenticationAgainstProtection

我已经为我的应用程序中的NSURLConnection问题挣扎了几天。我需要使用ftp协议连接到服务器(调用getPdfFromServerAtIndex),以获取对象(pdf页面,但这并不重要!)。当我尝试连接到http url时,我没有问题。对于请求身份验证的http url,所有委托方法都会被调用

但是,当我使用下面显示的url(使用ftp协议构建)时,像didReceiveAuthenticationChallenge、CanAuthenticationAgainstProtectionSpace这样的委托方法永远不会被调用

当“while(!finished)”循环时,只调用connectionShouldUseCredentialStorage方法,然后调用didFailWithError方法,并给出错误“Connection failed!error-您没有访问请求的资源的权限。”。然而,当我尝试在浏览器上粘贴ftp url时,我被要求填写用户名和密码

知道为什么使用ftp协议我不能认证和访问服务器吗

-(void)getPdfFromServerAtIndex:(NSUInteger)index{

// Create the request.
// Set finished to false
finished = FALSE;

NSString *pageNumber = [NSString stringWithFormat:@"ftp://ftp.www.thephilosopher.org/Iphone/0%i.pdf",index+1];

NSURL* nsurl = [NSURL URLWithString:pageNumber];
urlReq = [[NSMutableURLRequest alloc] initWithURL:nsurl];
BOOL canHandle = [NSURLConnection canHandleRequest:urlReq];


NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];


if (conn && canHandle) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
    NSLog(@"Connection Failed!");
}


while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];}
}  

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
finished = TRUE;

// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

// release the connection, and the data object
[connection release];
}  

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)tmpProtectionSpace {
// Check the NSURLProtectionSpace
return YES;}  

-(NSURLRequest *)connection:(NSURLConnection *)inConnection willSendRequest:(NSURLRequest *)inRequest redirectResponse:(NSURLResponse *)inRedirectResponse{       
if(inRedirectResponse){
    NSMutableURLRequest *r = [[urlReq mutableCopy] autorelease]; // original request
    [r setURL: [inRequest URL]];
    return r;
} else {
    return inRequest;
}
}  

-(BOOL)connectionShouldUseCredentialStorage:(NSURLConnection*)connection{
 NSLog(@"connectionShouldUseCredentialStorage");
 return YES;
}  

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if([challenge previousFailureCount] == 0) {
    NSURLCredential *newCredential;
    newCredential=[NSURLCredential credentialWithUser:@"XXXX" password:@"XXXXX" persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Bad Username Or Password");}
}  

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];

// inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}  

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

NSLog(@"Data received");
[receivedData appendData:data];
}  

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"Response received");
[receivedData setLength:0];
}  

只需以“格式”发布NSURLftp://username:password@ipaddress/filepath/“


对你来说:

我有最新消息。使用ASIFormDataRequest,我可以使用用户名和密码轻松访问服务器。为什么它不能与NSURLConnection一起工作?!请帮忙。