如何使用AFNetworking 3.0在后台下载大文件,并在会话完成所有任务时显示本地通知

时间:2016-02-11 15:06:58

标签: ios iphone afnetworking nsurlsessiondownloadtask afnetworking-3

我正在尝试使用AFNetworking 3.0在我的应用程序中下载大型视频文件。 我找不到任何关于如何使用AFNetworking在后台下载文件的具体文档。

我知道AFNetworking使用 AFURLSessionManager AFHTTPSessionManager ,它本身实现了 NSURLSession 的所有代表。

到目前为止,我创建了一个 backgroundSessionConfugaration 并启动了一个文件,可以使用它来下载。

self.manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:bgDownloadSessionIdentifier]];

[self.manager downloadTaskWithRequest:request progress:nil destination:nil completionHandler:nil];

下载开始时,我将我的应用程序放在后台。我到目前为止发现的是应用程序在后台运行直到20分钟并且下载成功。但是,appDelegate中的应用程序:handleEventsForBackgroundURLSession 未被调用,因此我的 setDidFinishEventsForBackgroundURLSessionBlock 也未被调用。

我想仅在调用 setDidFinishEventsForBackgroundURLSessionBlock 时显示通知。

当我使用此初始化管理器时。 (而不是 backgroundSessionConfiguration

self.manager = [AFHTTPSessionManager manager];

Still app能够在后台运行20分钟后下载整个文件。

我的问题是为什么应用程序运行到20分钟并完成下载。理想情况下iOS应该在10分钟后杀死应用程序。也是为什么在会话完成后没有调用应用程序:handleEventsForBackgroundURLSession

我已经提到了这个AFNetworking 2.0 and background transfers,但似乎对我不起作用。

功能的

后台模式是ON或OFF不会影响我的应用程序中的任何内容。

请帮助我或建议我在这里做错了什么。

我在这里上传了示例代码。 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;
 }

2 个答案:

答案 0 :(得分:3)

有时不会调用此委托方法,

  • 如果任务完成后应用程序已在运行。
  • 申请由主页按钮终止。
  • 如果您无法启动具有相同标识符的后台NSURLSession。

注意:这在模拟器上表现不同,所以请检查真实设备。

答案 1 :(得分:0)

为什么你没有在你的downloadInBackgroundWithURL中替换本地通知块到成功块:downloadProgress:fucntion? 真正没有调用AppDelegate中的方法。 您需要注册您的通知

- (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;
}

对于本地通知,添加开火日期和时区:

    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];

现在它有效。 欢呼声。