After I've read a bit of NSURLSession theory, I tried to write a small example.
#import “ExampleViewController.h"
@interface ExampleViewController () <NSURLSessionDelegate, NSURLSessionDownloadDelegate,NSURLSessionDataDelegate,NSURLSessionTaskDelegate>
@end
@implementation ExampleViewController
#pragma mark -
#pragma mark View Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"ftp://ftp.test.it/filedati.zip"];
NSString * utente = @"mickey";
NSString * codice = @"mouse";
NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url.host port:[url.port integerValue] protocol:url.scheme realm:nil authenticationMethod:nil];
NSURLCredential *cred = [NSURLCredential
credentialWithUser:utente
password:codice
persistence:NSURLCredentialPersistenceForSession];
NSURLCredentialStorage * cred_storage ;
[cred_storage setCredential:cred forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.URLCredentialStorage = cred_storage;
sessionConfiguration.allowsCellularAccess = YES;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSLog(@"viewdidload");
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
[downloadTask resume];
}
#pragma mark -
#pragma mark Session Download Delegate Methods
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(nullable NSError *)error {
NSLog(@"errors %@",error.debugDescription);
}
- (void)URLSession:(nonnull NSURLSession *)session task:(nonnull NSURLSessionTask *)task didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * __nullable))completionHandler
{
NSLog(@“credential”);
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSData *data = [NSData dataWithContentsOfURL:location];
dispatch_async(dispatch_get_main_queue(), ^{
[self.progressView setHidden:YES];
[self.imageView setImage:[UIImage imageWithData:data]];
});
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
NSLog(@"progress %f",progress);
dispatch_async(dispatch_get_main_queue(), ^{
[self.progressView setProgress:progress];
});
}
@end
Unfortunately, I get this error message
2015-06-22 20:54:07.383 Sessions[42566:6554516] viewdidload
2015-06-22 20:54:08.238 Sessions[42566:6554547] errori Error Domain=NSURLErrorDomain Code=-1102 "You do not have permission to access the requested resource." UserInfo=0x7fdcdaf2af70 {NSUnderlyingError=0x7fdcdaca4790 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1102.)", NSErrorFailingURLStringKey=ftp://ftp.test.it/filedati.zip", NSErrorFailingURLKey=ftp://ftp.test.it/filedati.zip, NSLocalizedDescription=You do not have permission to access the requested resource.}
I can not understand what's wrong