ftp簡介
什么是ftp
在iOS端的ftp上傳使用的是CFNetWork框架,它處于核心服務(wù)框架中,提供了一個抽象化的網(wǎng)絡(luò)協(xié)議庫啰劲。使用CFNetWork框架中的CFFTPStream類提供的API能夠做很多ftp操作,例如上傳文件交播、下載文件重虑、下載目錄列表,創(chuàng)建遠(yuǎn)程目錄等等秦士。
但是在使用上也存在一些的限制缺厉,CFFTPStream 提供的ftp操作比較基礎(chǔ),它并不是完全的FTP客戶端API隧土,不支持所有的FTP操作提针。
例如:
FTPS(FTP over TLS)
刪除文件(沒法像Android里面的comment-net包那樣可以直接刪除服務(wù)器文件)
重命名文件
自定義FTP命令
另外,CFFTPStream的目錄解析也局限于簡單的目錄曹傀,只能夠解析一般的基于UNIX FTP服務(wù)的目錄辐脖。
配置
要使用CFFTPStream的功能,需要導(dǎo)入CFNetWork 框架皆愉,方便的是不用去下載第三方包嗜价,直接在Xcode里面添加就好了。
1)添加CFNetWork framework
2)新建工程 使用Storyboard拖拽控件
協(xié)議
這里需要遵循 NSStreamDelegate
代碼演示
pragma mark 上傳事件
- (IBAction)sendAction:(id)sender {
NSURL *url;//ftp服務(wù)器地址
NSString *filePath;//圖片地址
NSString *account;//賬號
NSString *password;//密碼
CFWriteStreamRef ftpStream;
//獲得輸入
url = [NSURL URLWithString:_serverInput.text];
filePath = _fileInput.text;
account = _accountInput.text;
password = _passwordInput.text;
//添加后綴(文件名稱)
url = [NSMakeCollectable(CFURLCreateCopyAppendingPathComponent(NULL, (CFURLRef) url, (CFStringRef) [filePath lastPathComponent], false)) autorelease];
//讀取文件幕庐,轉(zhuǎn)化為輸入流
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
[self.fileStream open];
//為url開啟CFFTPStream輸出流
ftpStream = CFWriteStreamCreateWithFTPURL(NULL, (CFURLRef) url);
self.networkStream = (NSOutputStream *) ftpStream;
//設(shè)置ftp賬號密碼
[self.networkStream setProperty:account forKey:(id)kCFStreamPropertyFTPUserName];
[self.networkStream setProperty:password forKey:(id)kCFStreamPropertyFTPPassword];
//設(shè)置networkStream流的代理炭剪,任何關(guān)于networkStream的事件發(fā)生都會調(diào)用代理方法
self.networkStream.delegate = self;
//設(shè)置runloop
[self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.networkStream open];
//完成釋放鏈接
CFRelease(ftpStream);
}
pragma mark 回調(diào)方法
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
//aStream 即為設(shè)置為代理的networkStream
switch (eventCode) {
case NSStreamEventOpenCompleted: {
NSLog(@"NSStreamEventOpenCompleted");
} break;
case NSStreamEventHasBytesAvailable: {
NSLog(@"NSStreamEventHasBytesAvailable");
assert(NO); // 在上傳的時(shí)候不會調(diào)用
} break;
case NSStreamEventHasSpaceAvailable: {
NSLog(@"NSStreamEventHasSpaceAvailable");
NSLog(@"bufferOffset is %zd",self.bufferOffset);
NSLog(@"bufferLimit is %zu",self.bufferLimit);
if (self.bufferOffset == self.bufferLimit) {
NSInteger bytesRead;
bytesRead = [self.fileStream read:self.buffer maxLength:kSendBufferSize];
if (bytesRead == -1) {
//讀取文件錯誤
[self _stopSendWithStatus:@"讀取文件錯誤"];
} else if (bytesRead == 0) {
//文件讀取完成 上傳完成
[self _stopSendWithStatus:nil];
} else {
self.bufferOffset = 0;
self.bufferLimit = bytesRead;
}
}
if (self.bufferOffset != self.bufferLimit) {
//寫入數(shù)據(jù)
NSInteger bytesWritten;//bytesWritten為成功寫入的數(shù)據(jù)
bytesWritten = [self.networkStream write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset];
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self _stopSendWithStatus:@"網(wǎng)絡(luò)寫入錯誤"];
} else {
self.bufferOffset += bytesWritten;
}
}
} break;
case NSStreamEventErrorOccurred: {
[self _stopSendWithStatus:@"Stream打開錯誤"];
assert(NO);
} break;
case NSStreamEventEndEncountered: {
// 忽略
} break;
default: {
assert(NO);
} break;
}
}
//結(jié)果處理
(void)_stopSendWithStatus:(NSString *)statusString
{
if (self.networkStream != nil) {
[self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.networkStream.delegate = nil;
[self.networkStream close];
self.networkStream = nil;
}
if (self.fileStream != nil) {
[self.fileStream close];
self.fileStream = nil;
}
[self _sendDidStopWithStatus:statusString];
}(void)_sendDidStopWithStatus:(NSString *)statusString
{
if (statusString == nil) {
statusString = @"上傳成功";
}
_status.text = statusString;
}
//輸入Done事件
-(IBAction)textFieldDoneEditing:(id)sender{
[sender resignFirstResponder];
}
pragma mark UITextField代理方法
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[UIView beginAnimations:@"ResignForKeyBoard" context:nil];
[UIView setAnimationDuration:0.30f];
CGRect rect = CGRectMake(0.0f, -10.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[UIView beginAnimations:@"ResignForKeyBoard" context:nil];
[UIView setAnimationDuration:0.30f];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
return YES;
}