如何在 iOS objective C 用户强制退出应用程序的情况下继续在后台下载新图像?
How to keep downloading new images in background even if user force quits the app in iOS objective C?
我想下载300张左右的图片,并显示在UI中。
我有两个问题:
如果应用程序在前台,并且假设用户将其发送到后台模式(通过单击主页按钮),那么我如何确保下载继续进行?
如果用户强制退出应用程序(双击主页按钮并从应用程序切换器滑动应用程序),那么如何确保应用程序下载图像?
我一直在阅读有关背景知识的文章。很少有人说在 2. 情况下无法继续下载。
以下是我引用的链接:
http://www.appcoda.com/ios7-background-fetch-programming/
http://www.appcoda.com/background-transfer-service-ios7/
iOS Background downloads when the app is not active
我没有找到在前台下载图像的正确方法,background/suspended,用户强制退出应用程序。我正在使用 AFNetworking 进行网络服务调用。
下面是我的代码:
我已将 .json 文件中的 URL 之类的图像详细信息上传到亚马逊。要下载我做的文件,
[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://someurl/imageFile.json"]];
从这个文件中,我读取了图像的 url 并下载了它们以及图像详细信息。这是一个很大的过程。我该如何处理?请帮助..
您的 1 个问题的解决方案如下:
Background sessions let you perform uploads and downloads of content in the background while your app is not running. You can create a background session configuration by calling the backgroundSessionConfiguration: method on the NSURLSessionConfiguration class.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
NSURLSessionConfiguration *sessionConfig;
float timeout = 5 * 60.0f;
BOOL iOS8OrNew = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
if (iOS8OrNew) {
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
request.timeoutInterval = timeout;
}
else {
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
sessionConfig.timeoutIntervalForRequest = timeout;
}
sessionConfig.HTTPMaximumConnectionsPerHost = 10;
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfig];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request];
[manager setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession * _Nonnull session) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.backgroundSessionCompletionHandler) {
void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
appDelegate.backgroundSessionCompletionHandler = nil;
completionHandler();
}
NSLog(@"All tasks are finished");
}];
在您的 AppDelegate 中添加以下代码:
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler {
self.backgroundSessionCompletionHandler = completionHandler;
//add notification
[self presentNotification];
}
-(void)presentNotification{
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];
}
对于你的 2 解决方案
If the system kills your app and your background session has active downloads, your downloads will continue and the system will launch your app when the downloads complete. However, if a user force quits your app, all tasks get cancelled.
我想下载300张左右的图片,并显示在UI中。
我有两个问题:
如果应用程序在前台,并且假设用户将其发送到后台模式(通过单击主页按钮),那么我如何确保下载继续进行?
如果用户强制退出应用程序(双击主页按钮并从应用程序切换器滑动应用程序),那么如何确保应用程序下载图像?
我一直在阅读有关背景知识的文章。很少有人说在 2. 情况下无法继续下载。
以下是我引用的链接:
http://www.appcoda.com/ios7-background-fetch-programming/ http://www.appcoda.com/background-transfer-service-ios7/ iOS Background downloads when the app is not active
我没有找到在前台下载图像的正确方法,background/suspended,用户强制退出应用程序。我正在使用 AFNetworking 进行网络服务调用。
下面是我的代码:
我已将 .json 文件中的 URL 之类的图像详细信息上传到亚马逊。要下载我做的文件,
[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://someurl/imageFile.json"]];
从这个文件中,我读取了图像的 url 并下载了它们以及图像详细信息。这是一个很大的过程。我该如何处理?请帮助..
您的 1 个问题的解决方案如下:
Background sessions let you perform uploads and downloads of content in the background while your app is not running. You can create a background session configuration by calling the backgroundSessionConfiguration: method on the NSURLSessionConfiguration class.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
NSURLSessionConfiguration *sessionConfig;
float timeout = 5 * 60.0f;
BOOL iOS8OrNew = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
if (iOS8OrNew) {
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
request.timeoutInterval = timeout;
}
else {
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
sessionConfig.timeoutIntervalForRequest = timeout;
}
sessionConfig.HTTPMaximumConnectionsPerHost = 10;
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfig];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request];
[manager setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession * _Nonnull session) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.backgroundSessionCompletionHandler) {
void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
appDelegate.backgroundSessionCompletionHandler = nil;
completionHandler();
}
NSLog(@"All tasks are finished");
}];
在您的 AppDelegate 中添加以下代码:
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler {
self.backgroundSessionCompletionHandler = completionHandler;
//add notification
[self presentNotification];
}
-(void)presentNotification{
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];
}
对于你的 2 解决方案
If the system kills your app and your background session has active downloads, your downloads will continue and the system will launch your app when the downloads complete. However, if a user force quits your app, all tasks get cancelled.