3

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

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

到目前为止我所做的是,创建了一个backgroundSessionConfugaration并开始使用它下载一个文件。

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

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

下载开始时,我将应用程序置于后台。到目前为止,我发现应用程序在后台运行了 20 分钟,并且下载成功。但是appDelegate 中的application:handleEventsForBackgroundURLSession没有被调用,所以我的setDidFinishEventsForBackgroundURLSessionBlock也没有被调用。

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

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

self.manager = [AFHTTPSessionManager manager];

仍然应用程序能够在后台 20 分钟后下载整个文件。

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

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

功能中的后台模式打开或关闭不会影响我的应用程序中的任何内容。

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

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

2 回答 2

4

这个委托方法有时不会被调用,

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

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

于 2016-02-11T18:08:21.340 回答
0

为什么你只是没有在你的 downloadInBackgroundWithURL:downloadProgress: 功能中将本地通知块替换为成功块?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];

现在它起作用了。干杯。

于 2016-02-11T17:09:13.993 回答