Ios AFHTTPREquestOperation不';我不能在iPad上工作
我正试图在我的iPad上编译应用程序。我正在使用AFNetworking获取FTP上的文件列表。这个应用程序可以在模拟器上运行,但当我在iPad上启动它时,我会得到带有列表的文件内容(空)。以下是代码:Ios AFHTTPREquestOperation不';我不能在iPad上工作,ios,objective-c,ipad,ftp,afnetworking,Ios,Objective C,Ipad,Ftp,Afnetworking,我正试图在我的iPad上编译应用程序。我正在使用AFNetworking获取FTP上的文件列表。这个应用程序可以在模拟器上运行,但当我在iPad上启动它时,我会得到带有列表的文件内容(空)。以下是代码: - (void) getListOfFiles { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"ftp://anonymous@ftphost/"]]; AFHTTPRequestOp
- (void) getListOfFiles {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"ftp://anonymous@ftphost/"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *path = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"list"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSHTTPURLResponse *response) {
NSLog(@"Success %@",operation.response);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
[operation start];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog (@"%@",content);
}
所以变量内容=(null)只在iPad上,在模拟器上一切正常。请帮助,我已经失去了任何希望)默认情况下,AFHTTP*操作都是异步的 这是正确的,因为同步(阻塞)调用会阻塞主线程 您正在启动该操作,然后直接获取文件内容。这无法可靠地工作,因为启动调用是异步的,只启动op,不等待它 要么等待它这是坏的,因为它会阻塞线程:
[operation start];
[operation waitUntilDone];
或者更好将getFiles修改为异步工作:
- (void) getListOfFiles {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"URL"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *path = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"list"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSHTTPURLResponse *response) {
NSLog(@"Success %@",operation.response);
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog (@"%@",content);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
[operation start];
}
好的,问题解决了。我无法写入bundle目录,因此我需要使用下一个代码:
NSArray *paths = NSSearchPathForDirectoiesDomain(NSDocumentDirectory,NSCachesDirectory,YES);
NSString *path = [[paths objectAtIndex:0] stingByAppendingPathComponent:@"list"];
ftp服务器放在哪里?询问是否无法从设备访问ftp服务器…看起来您不完全理解异步回调。您需要读取成功块中的
path
的内容。可以从ipad(safari)访问ftp,我收到了成功消息,但文件仍然是blanc,我如何读取内容?好的,现在我收到了“success”消息,但内容仍然为空。您是异步还是使用wait hack执行的?:)刚刚用url=尝试过,它可以很好地与此代码配合使用。。当然要知道,如果服务器回答为nil,则内容为nil;)正如你们所说,我是异步的,但这怎么可能呢,那个应用程序在模拟器上工作(获取文件内容),在iPad上返回零?