[英]How to download large file in background using AFNetworking 3.0 and present local notification when session completes all the tasks
我正在嘗試使用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中的 application:handleEventsForBackgroundURLSession沒有被調用,所以我的setDidFinishEventsForBackgroundURLSessionBlock也沒有被調用。
我只想在調用setDidFinishEventsForBackgroundURLSessionBlock時顯示通知。
當我初始化經理使用此。 (而不是backgroundSessionConfiguration )
self.manager = [AFHTTPSessionManager manager];
Still應用程序能夠在后台運行20分鍾后下載整個文件。
我的問題是為什么應用程序要運行20分鍾並完成下載。 理想情況下,iOS應在10分鍾后終止該應用程序。 還有為什么會話完成后未調用application:handleEventsForBackgroundURLSession的原因。
我已經提到了AFNetworking 2.0和后台傳輸 ,但是它似乎對我不起作用。
功能的后台模式為“打開”或“關閉”不會影響我的應用程序。
請幫助我或建議我在這里做錯了什么。
我在這里上傳了示例代碼。 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;
}
有時不會調用此委托方法,
注意 :這在模擬器上的行為有所不同,因此請在真實設備上進行檢查。
為什么只在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];
現在可以了。 干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.