Jon*_*ebo 5 ftp xcode objective-c cfnetwork ios
我正在尝试使用FTP服务器.我已经搜索了所有内容,对于像我这样的初学者来说,一切都很难理解.SimpleFTPSample很难理解,因为它一次很多.视图,按钮,标签,textflelds,上传,下载,请求,列表,获取.与BlackRaccoon和其他一切相同.
如何简单和程序地将"test.txt"上传到FTP服务器:Xcode(iPhone应用程序)中的"192.168.1.111"没有视图或按钮.只是可以在ViewDidLoad中的代码.
也许这样的事情?:
NSURL* url = [NSURL URLWithString:@"ftp://username:pw@189.92.32.34"];
CFReadStreamRef stream = CFReadStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url);
stream.delegate= self;
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];
Run Code Online (Sandbox Code Playgroud)
但是哪个档案?展开它,或编写新代码.我不知道,这对我来说是新的.
谢谢乔纳森
作为Black Raccoon的作者,也许我有偏见(好吧,我知道我有偏见),但我试图让它变得尽可能简单和强大.让我们看看你想做什么,上传一个文件:
我们需要上传文件的四件事 - 启动代码,然后是四个委托方法:覆盖检查,数据,成功和失败.假设您将整个文件读入内存(对于小于2兆的小文件,可以).
首先,在标题中需要这个:
BRRequestUpload *uploadData; // Black Raccoon's upload object
NSData *uploadData; // data we plan to upload
Run Code Online (Sandbox Code Playgroud)
现在代码部分:
- (IBAction) uploadFile :(id)sender
{
//----- get the file path for the item we want to upload
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filepath = [NSString stringWithFormat: @"%@/%@", applicationDocumentsDir, @"file.text"];
//----- read the entire file into memory (small files only)
uploadData = [NSData dataWithContentsOfFile: filepath];
//----- create our upload object
uploadFile = [[BRRequestUpload alloc] initWithDelegate: self];
//----- for anonymous login just leave the username and password nil
uploadFile.path = @"/home/user/myfile.txt";
uploadFile.hostname = @"192.168.1.100";
uploadFile.username = @"yourusername";
uploadFile.password = @"yourpassword";
//----- we start the request
[uploadFile start];
}
Run Code Online (Sandbox Code Playgroud)
第一个是询问您的代码是否要覆盖现有文件.
-(BOOL) shouldOverwriteFileWithRequest: (BRRequest *) request
{
//----- set this as appropriate if you want the file to be overwritten
if (request == uploadFile)
{
//----- if uploading a file, we set it to YES (if set to NO, nothing happens)
return YES;
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,Black Raccoon将要求您提供要发送的数据块.如果你有一个非常大的文件,你永远不想尝试一次性发送它 - Apple的API将扼杀和丢弃数据.但是,我们只有一个小块,所以我们这样做:
- (NSData *) requestDataToSend: (BRRequestUpload *) request
{
//----- returns data object or nil when complete
//----- basically, first time we return the pointer to the NSData.
//----- and BR will upload the data.
//----- Second time we return nil which means no more data to send
NSData *temp = uploadData; // this is a shallow copy of the pointer
uploadData = nil; // next time around, return nil...
return temp;
}
Run Code Online (Sandbox Code Playgroud)
请记住,我们只能为一个小文件执行此操作.
接下来我们有完成处理程序(如果按照计划工作):
-(void) requestCompleted: (BRRequest *) request
{
if (request == uploadFile)
{
NSLog(@"%@ completed!", request);
uploadFile = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
最后我们有失败处理程序:
-(void) requestFailed:(BRRequest *) request
{
if (request == uploadFile)
{
NSLog(@"%@", request.error.message);
uploadFile = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
如果说的那么简单就会很棒[BRFtpUploadTo: dest srcfile: srcFile destfile: dstFile]但是你应该没有多少理由.其中一部分与Apple如何实施其内部FTP有关.还有阻塞,错误等问题.最后,FTP听起来应该是微不足道的,但最终有点像噩梦.
FTP是非常重要的,这就是为什么有这么多的实现.我不是在争论Black Raccoon是最好的,但是它会在几分钟到几天之间的问题上得到回应.
一开始可能看起来令人生畏,但在我看来,Black Raccoon是更好的FTP库之一.我花了很多时间和精力使它成为一个高质量的产品,对问题的反应非常好.我该如何免费这样做?体积.;)
祝你最终得到的FTP软件好运!
| 归档时间: |
|
| 查看次数: |
11849 次 |
| 最近记录: |