Reputation: 935
While I'm trying to upload a file through FTP using NSURLSession
I'm facing permission issue. But while trying with Core Foundation
framework file is not uploading but a file is getting created with the name I provided in code, even not facing any permission issue. For both I'm passing same credentials. If any one wants to see code please let me know I'll upload it. I want to add functionality of pause and resume too. So I'm not using any library even I want to implement it by myself so that I can learn about.
Upvotes: 0
Views: 480
Reputation: 566
In order to upload file to FTP Server, NSURLConnection cannot be used, instead SCRFTPConnection is used. You need to include CFNetwork framework. Also, below is the link to download SCRFTPRequest.h and SCRFTPRequest.m file from Github
https://github.com/Hackmodford/SCRFTPRequest
Copy just .h and .m file from the above zip file you download from Github and add it in your project.
// IN .H FILE OF YOUR VIEW CONTROLLER
Import the "SCRFTPRequest.h" file and add the delegate,
// IN .M FILE OF YOUR VIEW CONTROLLER
-(void)uploadFileUsingSCRFTPRequest{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file_name=[NSString stringWithFormat:@"TEST.txt"];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:file_name];
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://your_url/"] toUploadFile:myPathDocs];
ftpRequest.username = @"username";
ftpRequest.password = @"password";
ftpRequest.delegate = self;
[ftpRequest startRequest];
}
- (void)ftpRequestDidFinish:(SCRFTPRequest *)request {
NSLog(@"Upload finished.");
}
- (void)ftpRequest:(SCRFTPRequest *)request didFailWithError:(NSError *)error {
NSString *str=[NSString stringWithFormat:@"Upload failed: %@", [error localizedDescription]];
NSLog(@"Upload failed: %@", [error localizedDescription]);
}
Upvotes: 1