我試圖在應用程序內建立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