如何在ios中通过FTP上传文件?
我想在FTP服务器上上传文件。如何实现此功能 我尝试过很多库,比如raccoons,但这些库都有不推荐使用的代码,这些代码不起作用如何在ios中通过FTP上传文件?,ios,swift,Ios,Swift,我想在FTP服务器上上传文件。如何实现此功能 我尝试过很多库,比如raccoons,但这些库都有不推荐使用的代码,这些代码不起作用 NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password!@host:port/filename.wav"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload
NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password!@host:port/filename.wav"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload];
[request setHTTPMethod:@"PUT"];
NSString *file = [[NSBundle mainBundle] pathForResource:@"1kb" ofType:@"png"];
NSURL *docsDirURL = [NSURL fileURLWithPath:filePath];
NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url_upload.host port:[url_upload.port integerValue] protocol:url_upload.scheme realm:nil authenticationMethod:nil];
NSURLCredential *cred = [NSURLCredential
credentialWithUser:@"username"
password:@"pass!"
persistence:NSURLCredentialPersistenceForSession];
NSURLCredentialStorage * cred_storage ;
[cred_storage setCredential:cred forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.URLCredentialStorage = cred_storage;
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:docsDirURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error != NULL)
{
NSLog(@"%@", error.localizedDescription);
}
else{
}
}];
[uploadTask resume];
请向我推荐一些快速或客观的c实现,以便在FTP服务器上上载文件。您可以使用
你可以用
FTP应该起作用。不是测试。SFTP不会。但这不是问题。@Pinturajp但为什么不呢?不接受像FTPS这样的URL应该是可行的。不是测试。SFTP不会。但这不是问题。@Pinturajp但为什么不呢?不接受URL likeL
let returnCredential = URLCredential(user:<username>, password:<password>, persistence: .none)
let ftpFileProvider = FTPFileProvider(baseURL: URL(string:<host eg ftp:>, mode: .default, credential: returnCredential, cache: .none)!
ftpFileProvider.copyItem(localFile: <local_file_path>, to:<where on ftp>, completionHandler: { error in
if error != nil {
print(error)
return
}
})