AFNetworking 2.0.0 download in background - ios

I'm using 'AFNetworking', '2.0.0' for download data.
I need to download large file. When user lock screen or press home button it should go pause (or continue downloading in background) and if I return to app it should resume.
Also I need to show progress of downloading.
I search a lot of examples but don't find anything 'AFNetworking', '2.0.0'.
I create app for iOS version 6.0+, so I can't use AFHTTPSessionManager or AFURLSessionManager.

For downloading in background on iOS 7 or higher i'am using NSURLSession with their NSURLSessionDownloadTask.
Switch on BackgroundMode in ProjectNavigator->YourProject->YourTarget->Capabilities (tab)->Background Modes
Add to your AppDelegate method - (BOOL)application:(UIApplication* )application didFinishLaunchingWithOptions:(NSDictionary* )launchOptions an initialiser for NSURLSessin with next code:
NSURLSessionConfiguration *sessionConfiguration;
NSString *someUniqieIdentifierForSession = #"com.etbook.background.DownloadManager";
if ([[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:#"."] firstObject] integerValue] >= 8) {
//This code for iOS 8 and greater
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:someUniqieIdentifierForSession];
} else {
this code for iOS 7
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:someUniqieIdentifierForSession];
}
sessionConfiguration.HTTPMaximumConnectionsPerHost = 5;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
Of course, don't forget to declare session property with:
#property (nonatomic, strong) NSURLSession session;
Also add completion handler property (it will be needed if you want to get callback of background download process after Application termination or crash):
#property (nonatomic, copy) void(^backgroundTransferCompletionHandler)();
Add delegate methods in AppDelegate:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destinationFilename = downloadTask.originalRequest.URL.lastPathComponent;
NSURL *destinationURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:destinationFilename];
if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
}
[fileManager copyItemAtURL:location
toURL:destinationURL
error:&error];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
if (error != nil) {
NSLog(#"Download completed with error: %#", [error localizedDescription]);
}
else{
NSLog(#"Download finished successfully.");
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
NSLog(#"Unknown transfer size");
}
else{
NSLog(#"progress = %lld Mb of %lld Mb", totalBytesWritten/1024/1024, totalBytesExpectedToWrite/1024/1024);
}
}
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
ETBAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
// Check if all download tasks have been finished.
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
if (appDelegate.backgroundTransferCompletionHandler != nil) {
// Copy locally the completion handler.
void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;
// Make nil the backgroundTransferCompletionHandler.
appDelegate.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Call the completion handler to tell the system that there are no other background transfers.
completionHandler();
// Show a local notification when all downloads are over.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"All files have been downloaded!";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}];
}
}
}];
}
//Background download support (THIS IMPORTANT METHOD APPLICABLE ONLY IN YOUR AppDelegate.m FILE!!!)
-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler{
self.backgroundTransferCompletionHandler = completionHandler;
}
And declare that your class (in AppDelegate.h file for my example) conforms to protocol NSURLSessionDelegate like here:
#interface AppDelegate : UIResponder <UIApplicationDelegate, NSURLSessionDelegate>
Then add download task somewhere with:
NSURLSessionDownloadTask *task = [self.session downloadTaskWithURL:[NSURL URLWithString:urlString]];
task.taskDescription = [NSString stringWithFormat:#"Downloading file %#", [urlString lastPathComponent]];
[task resume];
So when your application started after termination, your session will be restored and delegate methods will be fired. Also if your application will waked up from background to the foreground your application delegate methods will be fired too.

when your app goes to background pause/stop the downloading operation and when comes in foreground resume your paused downloading operation in NSOperation queue.
Read these lines of code may help you:
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:filePath shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//handel completion
[operations removeObjectForKey:audioItem.filename];
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//handel failure
operation = [operations objectForKey:audioItem.filename];
[operation cancel];
[operations removeObjectForKey:audioItem.filename];
if (error.code != NSURLErrorCancelled) {
[self showErrorAlert];
NSLog(#"Error: %#", error);
}
}];
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
//handel progress
NSNumber *progress = [NSNumber numberWithFloat:((float)totalBytesReadForFile / (float)totalBytesExpectedToReadForFile)];
float progress=progress.floatValue;
[self performSelectorOnMainThread:#selector(updateProgressBar:) withObject:[NSArray arrayWithObjects:audioItem, progress, nil] waitUntilDone: YES];
}];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
// TODO: Clean up operations
NSLog(#"App has been in background too long, need to clean up any connections!");
}];
[operations setValue:operation forKey:#"key"]; // store the NSOperation for further use in application to track its status.
[downloadQueue addOperation:operation];//here downloadQueue is NSOperationQueue instance that manages NSOperations.
To Pause downloading operation use this:
[operation pause];
To resume use this sort of line:
[operation resume];

Perhaps you're better off using DTDownload:
https://github.com/Cocoanetics/DTDownload
As far as I can tell, it doesn't have anything to report progress.
Edit: there's a nice tutorial over at AppCoda, about using NSURLSessionTask:
http://www.appcoda.com/background-transfer-service-ios7/

Related

How to download large file in background using AFNetworking 3.0 and present local notification when session completes all the tasks

I am trying to download large video file in my application using AFNetworking 3.0.
I couldn't find any specific document regarding how to download file in background using AFNetworking.
I know AFNetworking uses AFURLSessionManager and AFHTTPSessionManager which itself implements all the delegates of NSURLSession.
What I did so far is, created a backgroundSessionConfugaration and started a file to download using this.
self.manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:bgDownloadSessionIdentifier]];
[self.manager downloadTaskWithRequest:request progress:nil destination:nil completionHandler:nil];
When download starts, I put my application in background. What I found so far is app is running in background till 20 mins and download was successful. But application:handleEventsForBackgroundURLSession in appDelegate is not called, so my setDidFinishEventsForBackgroundURLSessionBlock is also not getting called.
I want to show a notification only when setDidFinishEventsForBackgroundURLSessionBlock is called.
When I initialise manager using this. (instead of backgroundSessionConfiguration)
self.manager = [AFHTTPSessionManager manager];
Still app is able to download entire file after 20 mins in background.
My problem is why application is running till 20 mins and completed the download. As ideally iOS should kill the app after 10 mins. Also why application:handleEventsForBackgroundURLSession is not getting called after session completion.
I've already referred this AFNetworking 2.0 and background transfers, but it seems not working for me.
Background Modes from capabilities is either ON or OFF doesn't affect anything in my application.
Please help me or suggest me what I am doing wrong here.
I've uploaded the example code here.
https://www.dropbox.com/s/umwicuta2qzd3k1/RSNetworkKitExample.zip?dl=0
AppDelegate.m
-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
NSLog(#"===== background session completed in AppDelegate ===== ");
if([identifier isEqualToString:bgDownloadSessionIdentifier]){
[RSDownloadManager sharedManager].backgroundSessionCompletionHandler = completionHandler;
}
}
====================================================
ViewController.m
- (IBAction)downloadVideoInBackground:(id)sender {
NSString *videoURL = #"http://videos1.djmazadownload.com/mobile/mobile_videos/Tum%20Saath%20Ho%20(Tamasha)%20(DJMaza.Info).mp4";
[RSDownloadManager sharedManager].backgroundSessionCompletionHandler = ^{
NSLog(#"===== background session completed in ViewController ===== ");
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Download Complete!";
localNotification.alertAction = #"Background Transfer Download!";
//On sound
localNotification.soundName = UILocalNotificationDefaultSoundName;
//increase the badge number of application plus 1
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
};
[[RSDownloadManager sharedManager] downloadInBackgroundWithURL:videoURL downloadProgress:^(NSNumber *progress) {
NSLog(#"Video download progress %.2f", [progress floatValue]);
} success:^(NSURLResponse *response, NSURL *filePath) {
NSLog(#"Video download completed at Path %#", filePath);
} andFailure:^(NSError *error) {
NSLog(#"Error in video download %#", error.description);
}];
}
====================================================
RSDownloadManager.h
#import <Foundation/Foundation.h>
#import "AFHTTPSessionManager.h"
static NSString *bgDownloadSessionIdentifier = #"com.RSNetworkKit.bgDownloadSessionIdentifier";
#interface RSDownloadManager : NSObject
#property (nonatomic, strong) AFHTTPSessionManager *manager;
#property (nonatomic, copy) void (^backgroundSessionCompletionHandler)(void);
+(instancetype)sharedManager;
-(NSURLSessionDownloadTask *)downloadInBackgroundWithURL:(NSString *)urlString downloadProgress:(void (^)(NSNumber *progress))progressBlock success:(void (^)(NSURLResponse *response, NSURL *filePath))completionBlock andFailure:(void (^)(NSError *error))failureBlock;
#end
====================================================
RSDownloadManager.m
#implementation RSDownloadManager
#pragma mark - Singleton instance
+(instancetype)sharedManager
{
static RSDownloadManager *_downloadManager = nil;
static dispatch_once_t token;
dispatch_once(&token, ^{
if (!_downloadManager) {
_downloadManager = [[self alloc] init];
}
});
return _downloadManager;
}
#pragma mark - Init with Session Configuration
-(void)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
self.manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
//self.manager = [AFHTTPSessionManager manager];
}
#pragma mark- Handle background session completion
- (void)configureBackgroundSessionCompletion {
typeof(self) __weak weakSelf = self;
[self.manager setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession *session) {
if (weakSelf.backgroundSessionCompletionHandler) {
weakSelf.backgroundSessionCompletionHandler();
weakSelf.backgroundSessionCompletionHandler = nil;
}
}];
}
#pragma mark- download in background request Method
-(NSURLSessionDownloadTask *)downloadInBackgroundWithURL:(NSString *)urlString downloadProgress:(void (^)(NSNumber *))progressBlock success:(void (^)(NSURLResponse *, NSURL *))completionBlock andFailure:(void (^)(NSError *))failureBlock {
/* initialise session manager with background configuration */
if(!self.manager){
[self initWithSessionConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:bgDownloadSessionIdentifier]];
}
[self configureBackgroundSessionCompletion];
/* Create a request from the url */
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLSessionDownloadTask *downloadTask = [self.manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
if(progressBlock) {
progressBlock ([NSNumber numberWithDouble:downloadProgress.fractionCompleted]);
}
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if(error) {
if(failureBlock) {
failureBlock (error);
}
}
else {
if(completionBlock) {
completionBlock (response, filePath);
}
}
}];
[downloadTask resume];
return downloadTask;
}
This delegate methods will not get called sometimes ,
If application is already running when task completes.
application terminated by home button.
If you fail to start a background NSURLSession with the same identifier.
Note : this behaves differently on simulator, so please check on real device.
Why you just no replace local notification block to success block in your downloadInBackgroundWithURL:downloadProgress: fucntion?
Method in AppDelegate realy not called.
And you need register your notification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
And for local notification add fire date and time zone:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Download Complete!";
localNotification.alertAction = #"Background Transfer Download!";
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
//On sound
localNotification.soundName = UILocalNotificationDefaultSoundName;
//increase the badge number of application plus 1
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
Now it works.
Cheers.

iOS Memory management issue with ARC

I've created a class called "ConnectionManager" that will handle all network request and fetch data from the server and after that call to a completion handler.
ConnectionManager.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UIAlertView+CustomAlertView.h"
#import <Crashlytics/Crashlytics.h>
#interface ConnectionManager : NSObject<NSURLSessionDataDelegate>
#property (weak, nonatomic) NSMutableData *receivedData;
#property (weak, nonatomic) NSURL *url;
#property (weak, nonatomic) NSURLRequest *uploadRequest;
#property (nonatomic, copy) void (^onCompletion)(NSData *data);
#property BOOL log;
-(void)downloadDataFromURL:(NSURL *)url withCompletionHandler:(void (^)(NSData *data))completionHandler;
-(void)uploadDataWithRequest:(NSURLRequest*)request withCompletionHandler:(void (^)(NSData *data))completionHandler;
#end
ConnectionManager.m
#import "ConnectionManager.h"
#implementation ConnectionManager
-(void)uploadDataWithRequest:(NSURLRequest*)request withCompletionHandler:(void (^)(NSData *data))completionHandler{
// Instantiate a session configuration object.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
// Configure Session Configuration
[configuration setAllowsCellularAccess:YES];
// Instantiate a session object.
NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
// Assign request for later call
self.uploadRequest = request;
// Create an upload task object to perform the data uploading.
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:self.uploadRequest fromData:nil];
// Assign completion handler
self.onCompletion = completionHandler;
// Inititate data
self.receivedData = [[NSMutableData alloc] init];
// Resume the task.
[task resume];
}
-(void)downloadDataFromURL:(NSURL *)url withCompletionHandler:(void (^)(NSData *data))completionHandler{
// Instantiate a session configuration object.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
// Configure Session Configuration
[configuration setAllowsCellularAccess:YES];
// Instantiate a session object.
NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
// Assign url for later call
self.url = url;
// Create a data task object to perform the data downloading.
NSURLSessionDataTask *task = [session dataTaskWithURL:self.url];
// Assign completion handler
self.onCompletion = completionHandler;
// Inititate data
self.receivedData = [[NSMutableData alloc] init];
// Resume the task.
[task resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error) {
if (error.code == -1003 || error.code == -1009) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Unable to connect to the server. Please check your internet connection and try again!" delegate:nil cancelButtonTitle:#"cancel" otherButtonTitles:#"retry",nil];
[alert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex==1) {
// Retry
if (self.url) {
NSURLSessionDataTask *retryTask = [session dataTaskWithURL:self.url];
[retryTask resume];
}else{
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:self.uploadRequest fromData:nil];
[task resume];
}
}else{
self.onCompletion(nil);
}
}];
}];
}else{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"An unkown error occured! Please try again later, thanks for your patience." delegate:nil cancelButtonTitle:#"cancel" otherButtonTitles:#"retry",nil];
[alert show];
CLS_LOG(#"Error details: %#",error);
self.onCompletion(nil);
}];
}
}
else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.onCompletion(self.receivedData);
}];
}
}
#end
Here is the piece of code I use to call it :
-(void)loadDataFromServer{
NSString *URLString = [NSString stringWithFormat:#"%#get_people_number?access_token=%#", global.baseURL, global.accessToken];
URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:URLString];
ConnectionManager *connectionManager = [[ConnectionManager alloc] init];
[connectionManager downloadDataFromURL:url withCompletionHandler:^(NSData *data) {
if (data != nil) {
// Convert the returned data into an array.
NSError *error;
NSDictionary *number = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) {
CLS_LOG(#"Error: %#, Response:%#",error,[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}
else{
[_mapView updateUIWithData:[number objectForKey:#"number"]];
}
}
}];
}
I figured out when using Instruments that All objects of ConnectionManager type are persistent even after getting the data from server and calling the completion handler.
I tried to change the completion handler property from copy to strong, but I got the same results. Changing it to weak cause a crash and it never be called.
Please someone guide me to the right way.
After doing a lot of research, I found that the URL Session object is still alive.
Based on Apple Documentation Life Cycle of URL Session there's two cases:
Using the system provided delegate (by not setting a delegate), here the system will take care of invalidating the NSURLSession object.
Using a custom delegate. When you no longer need a session, invalidate it by calling either invalidateAndCancel (to cancel outstanding tasks) or finishTasksAndInvalidate (to allow outstanding tasks to finish before invalidating the object).
To fix the code above I only changed the delegate method "didCompleteWithError" like the following:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error) {
if (error.code == -1003 || error.code == -1009) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Unable to connect to the server. Please check your internet connection and try again!" delegate:nil cancelButtonTitle:#"cancel" otherButtonTitles:#"retry",nil];
[alert showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex==1) {
// Retry
if (self.url) {
NSURLSessionDataTask *retryTask = [session dataTaskWithURL:self.url];
[retryTask resume];
}else{
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:self.uploadRequest fromData:nil];
[task resume];
}
}else{
[session finishTasksAndInvalidate];
self.onCompletion(nil);
}
}];
}];
}else{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"An unkown error occured! Please try again later, thanks for your patience." delegate:nil cancelButtonTitle:#"cancel" otherButtonTitles:#"retry",nil];
[alert show];
[session finishTasksAndInvalidate];
self.onCompletion(nil);
}];
}
}
else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[session finishTasksAndInvalidate];
self.onCompletion(self.receivedData);
}];
}
}

how to cancel the pending download task using NSURLSession in ios?

I am creating a iPhone app which download icons using NSURLSession & NSURLDownloadTask.
In this case i want to download only the visible cell icons, when i scrolled the table all pending downloadTask get canceled (i.e download task for non-visible cells is must cancel). For this i created a method terminateAllDownloads().
// terminateAllDownloadTask
-(void)terminateAllDownloads
{
NSArray *allDownloads = [self.iconDownloadInProgress allValues];
[allDownloads makeObjectsPerformSelector:#selector(cancelDownload)];
[self.iconDownloadInProgress removeAllObjects];
}
// cancelDownload
-(void)cancelDownload
{
[self.downloadTask cancel];
[self.session invalidateAndCancel];
}
and call this function in dealloc & didRecieveMemoryWarning methods of MasterViewController.m
// dealloc
-(void)dealloc
{
[self terminateAllDownloads];
}
// didReceiveMemeoryWarning
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[self terminateAllDownloads];
}
but this method is not called single time. how should i do this ?
I am using the NSURLSession Delegates, not completion block
//code
-(void)startDownload
{
appDelgate = [[UIApplication sharedApplication] delegate];
self.fileName = self.appData.name;
self.iconURL = self.appData.iconURL;
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.allowsCellularAccess = NO;
_session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
self.downloadTask = [_session downloadTaskWithURL:[NSURL URLWithString:_iconURL]];
[_downloadTask resume];
}
-(void)cancelDownload
{
[self.downloadTask cancel];
[self.session invalidateAndCancel];
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSString *trimmedString = [_fileName stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *appIconDirectory = [[documentsDirectoryForAppIcons absoluteString] stringByAppendingPathComponent:#"appIcons"];
NSURL* destinationUrlForAppIcon = [[NSURL URLWithString:appIconDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:#"%#%#",trimmedString, #".png"]];
NSError *error1;
if([appIconFileManager fileExistsAtPath:[destinationUrlForAppIcon absoluteString]])
{
[appIconFileManager removeItemAtPath:[destinationUrlForAppIcon absoluteString] error:NULL];
}
BOOL status = [appIconFileManager copyItemAtURL:location toURL:destinationUrlForAppIcon error:&error1];
if (status && !error1)
{
[appDelgate.downloadedIcons setValue:destinationUrlForAppIcon.path forKey:self.iconURL];
if(self.completionHandler)
{
self.completionHandler(destinationUrlForAppIcon);
}
}
else
{
NSLog(#"File copy failed: %#", [error1 localizedDescription]);
}
}
Probably you refer to self in the block passed to the NSURLSession producing a retain cycle.

iOS NSURLSession Download

I got this code to implement something which helps me downloading a file from a given URL.
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(#"Temporary File :%#\n", location);
NSError *err = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:#"out1.zip"]];
if ([fileManager moveItemAtURL:location
toURL:docsDirURL
error: &err])
{
NSLog(#"File is saved to =%#",docsDir);
}
else
{
NSLog(#"failed to move: %#",[err userInfo]);
}
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
//You can get progress here
NSLog(#"Received: %lld bytes (Downloaded: %lld bytes) Expected: %lld bytes.\n",
bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}
Second part:
-(void) downloadFileWithProgress
{
NSURL * url = [NSURL URLWithString:#"https://s3.amazonaws.com/hayageek/downloads/SimpleBackgroundFetch.zip"];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDownloadTask * downloadTask =[ defaultSession downloadTaskWithURL:url];
[downloadTask resume];
}
All of this code is in my Download.m
My download.h is:
#interface Download : NSObject
-(void) downloadFileWithProgress
#end
I really dont know how to get the download starting. In another class I created a button which should start the download:
-(IBAction)buttonStartDownload:(id)sender {
[Download downloadFileWithProgress];
}
The error is in the last line:
No known class method for selector 'downloadFileWithProgress'
But why?
The method '-(void) downloadFileWithProgress' is an instance method so you cant call this method by using class name 'Download'.
In order to call this method you need to create an instance of 'Download' class and call the method on that instance.
Method -(void)downloadFilwWithProgress in instance method...So to call that method
-(IBAction)buttonStartDownload:(id)sender {
Download *downldObj=[[Download alloc]init];
[downldObj downloadFileWithProgress];
}
If you write method +(void)downloadFilwWithProgress then you can call like this.[Download downloadFileWithProgress]

How to get server response data in NSURLSession without completion block

I am using NSURLSession for background image uploading. And according to uploaded image my server gives me response and I do change in my app accordingly. But I can't get my server response when my app uploading image in background because there is no completion block.
Is there way to get response without using completion block in NSURLUploadTask?
Here is my code :
self.uploadTask = [self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"returnString : %#",returnString);
NSLog(#"error : %#",error);
}];
[self.uploadTask resume];
But i got this error..
Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'
But if I can't use completion handler than how should I get the server response. It says use delegate but I can't find any delegate method which can gives me server response.
A couple of thoughts:
First, instantiate your session with a delegate, because background sessions must have a delegate:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:kSessionIdentifier];
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
Second, instantiate your NSURLSessionUploadTask without a completion handler, because tasks added to a background session cannot use completion blocks. Also note, I'm using a file URL rather than a NSData:
NSURLSessionTask *task = [self.session uploadTaskWithRequest:request fromFile:fileURL];
[task resume];
Third, implement the relevant delegate methods. At a minimum, that might look like:
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSMutableData *responseData = self.responsesData[#(dataTask.taskIdentifier)];
if (!responseData) {
responseData = [NSMutableData dataWithData:data];
self.responsesData[#(dataTask.taskIdentifier)] = responseData;
} else {
[responseData appendData:data];
}
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error) {
NSLog(#"%# failed: %#", task.originalRequest.URL, error);
}
NSMutableData *responseData = self.responsesData[#(task.taskIdentifier)];
if (responseData) {
// my response is JSON; I don't know what yours is, though this handles both
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
if (response) {
NSLog(#"response = %#", response);
} else {
NSLog(#"responseData = %#", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
[self.responsesData removeObjectForKey:#(task.taskIdentifier)];
} else {
NSLog(#"responseData is nil");
}
}
Note, the above is taking advantage of a previously instantiated NSMutableDictionary called responsesData (because, much to my chagrin, these "task" delegate methods are done at the "session" level).
Finally, you want to make sure to define a property to store the completionHandler provided by handleEventsForBackgroundURLSession:
#property (nonatomic, copy) void (^backgroundSessionCompletionHandler)(void);
And obviously, have your app delegate respond to handleEventsForBackgroundURLSession, saving the completionHandler, which will be used below in the URLSessionDidFinishEventsForBackgroundURLSession method.
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
// This instantiates the `NSURLSession` and saves the completionHandler.
// I happen to be doing this in my session manager, but you can do this any
// way you want.
[SessionManager sharedManager].backgroundSessionCompletionHandler = completionHandler;
}
And then make sure your NSURLSessionDelegate calls this handler on the main thread when the background session is done:
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
if (self.backgroundSessionCompletionHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
self.backgroundSessionCompletionHandler();
self.backgroundSessionCompletionHandler = nil;
});
}
}
This is only called if some of the uploads finished in the background.
There are a few moving parts, as you can see, but that's basically what's entailed.

Resources