我试图在应用程序内建立FTP连接。我想上传几个文件到一个FTP服务器,所有文件在一个目录中。所以一开始我想创建远程目录。通过iPhone上的ftp在后台上传文件
- (void) createRemoteDir {
NSURL *destinationDirURL = [NSURL URLWithString: uploadDir];
CFWriteStreamRef writeStreamRef = CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) destinationDirURL);
assert(writeStreamRef != NULL);
ftpStream = (__bridge_transfer NSOutputStream *) writeStreamRef;
BOOL success = [ftpStream setProperty: ftpUser forKey: (id)kCFStreamPropertyFTPUserName];
if (success) {
NSLog(@"\tsuccessfully set the user name");
}
success = [ftpStream setProperty: ftpPass forKey: (id)kCFStreamPropertyFTPPassword];
if (success) {
NSLog(@"\tsuccessfully set the password");
}
ftpStream.delegate = self;
[ftpStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// open stream
[ftpStream open];
}
时在后台线程中使用以下调用执行该代码不起作用:
[self performSelectorInBackground: @selector(createRemoteDir) withObject: nil];
我的猜测是,(后台线程)runloop没有运行? 如果我发主线程内的信息上传只是正常工作:
[self createRemoteDir];
主线程的runloop启动并运行。
但相当大的文件将被上传;所以我想把这个工作负载放在后台线程中。 但我如何以及在哪里设置NSRunLoop,以便整个上传发生在后台线程中?苹果关于NSRunLoops的文档(特别是如何在不使用定时器/输入源的情况下启动它们,就像这种情况一样)没有帮助我。
我在这里发现了一个类似的问题;我真的很想看到它的解决方案的代码:[链接](http://stackoverflow.com/questions/4930957/nsstream-and-sockets-nsstreamdelegate-methods-not-being-called) – 2012-02-05 17:15:58