iOS FTP上傳

ftp簡介

什么是ftp

在iOS端的ftp上傳使用的是CFNetWork框架,它處于核心服務框架中,提供了一個抽象化的網(wǎng)絡協(xié)議庫。使用CFNetWork框架中的CFFTPStream類提供的API能夠做很多ftp操作,例如上傳文件、下載文件、下載目錄列表,創(chuàng)建遠程目錄等等。

但是在使用上也存在一些的限制,CFFTPStream 提供的ftp操作比較基礎,它并不是完全的FTP客戶端API,不支持所有的FTP操作。
例如:

FTPS(FTP over TLS)
刪除文件(沒法像Android里面的comment-net包那樣可以直接刪除服務器文件)
重命名文件
自定義FTP命令
另外,CFFTPStream的目錄解析也局限于簡單的目錄,只能夠解析一般的基于UNIX FTP服務的目錄。

配置

要使用CFFTPStream的功能,需要導入CFNetWork 框架,方便的是不用去下載第三方包,直接在Xcode里面添加就好了。

1)添加CFNetWork framework

2)新建工程 使用Storyboard拖拽控件

協(xié)議

這里需要遵循 NSStreamDelegate

代碼演示

pragma mark 上傳事件

  • (IBAction)sendAction:(id)sender {

NSURL *url;//ftp服務器地址
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;

//設置ftp賬號密碼
[self.networkStream setProperty:account forKey:(id)kCFStreamPropertyFTPUserName];
[self.networkStream setProperty:password forKey:(id)kCFStreamPropertyFTPPassword];

//設置networkStream流的代理,任何關(guān)于networkStream的事件發(fā)生都會調(diào)用代理方法
self.networkStream.delegate = self;

//設置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 即為設置為代理的networkStream
    switch (eventCode) {
    case NSStreamEventOpenCompleted: {
    NSLog(@"NSStreamEventOpenCompleted");
    } break;
    case NSStreamEventHasBytesAvailable: {
    NSLog(@"NSStreamEventHasBytesAvailable");
    assert(NO); // 在上傳的時候不會調(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)絡寫入錯誤"];
} 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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容