即使用户强制退出 iOS Objective C 中的应用程序,如何在后台继续下载新图像?

2024-01-31

我想下载300张左右的许多图像,并将其显示在UI中。

我有两个问题:

  1. 如果应用程序位于前台,并且假设用户将其发送到后台模式(通过单击主页按钮),那么我如何确保下载继续?

  2. 如果用户强制退出应用程序(双击主页按钮并从应用程序切换器中滑动应用程序),那么如何确保应用程序下载图像?

我读了很多关于背景的东西。很少有人还说,在 2. 情况下,下载无法继续。

以下是我参考的链接:

http://www.appcoda.com/ios7-background-fetch-programming/ http://www.appcoda.com/ios7-background-fetch-programming/ http://www.appcoda.com/background-transfer-service-ios7/ http://www.appcoda.com/background-transfer-service-ios7/ iOS 应用程序未激活时后台下载 https://stackoverflow.com/questions/8861390/ios-background-downloads-when-the-app-is-not-active

我没有找到正确的方法来下载前台、后台/挂起的图像,用户强制退出应用程序。我正在使用 AFNetworking 进行网络服务调用。

下面是我的代码:

我已将图像详细信息(例如 .json 文件中的 URL)上传到亚马逊。要下载我做的文件,

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://someurl/imageFile.json"]];

从这个文件中,我读取了图像的 URL,并将其与图像详细信息一起下载。这是一个很大的过程。我该如何处理?请帮忙..


您的 1 个问题的解决方案如下:

后台会话允许您在应用程序未运行时在后台执行内容的上传和下载。您可以通过调用 NSURLSessionConfiguration 类上的 backgroundSessionConfiguration: 方法来创建后台会话配置。

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解决方案

如果系统终止您的应用程序并且您的后台会话有活动下载,您的下载将继续,系统将在下载完成后启动您的应用程序。但是,如果用户强制退出您的应用程序,所有任务都会被取消。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

即使用户强制退出 iOS Objective C 中的应用程序,如何在后台继续下载新图像? 的相关文章

随机推荐