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

即使用户强制退出iOS objective C中的应用程序,如何在后台继续下载新图像?,ios,objective-c,download,background,afnetworking,Ios,Objective C,Download,Background,Afnetworking,我想下载大约300张图片,并在UI中显示 我有两个问题: - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler { self.backgroundSessionCompletionHandler = completionHandl

我想下载大约300张图片,并在UI中显示

我有两个问题:

 - (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];
}
  • 如果应用程序位于前台,并且假设用户将其发送到后台模式(通过单击主页按钮),那么如何确保下载继续

  • 如果用户强制退出应用程序(双击home按钮并从app switcher中滑动应用程序),那么如何确保应用程序下载图像

  • 我一直在读很多背景资料。很少有人会这样说。案例下载无法继续

    以下是我提到的链接:

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

    我没有得到正确的方法来下载前景、背景/挂起的图像,用户强制退出应用程序。我正在使用AFNetworking进行Web服务呼叫

    下面是我的代码:

     - (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];
    }
    
    我已经将.json文件中的URL等图像详细信息上传到amazon。要下载我需要的文件

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

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

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

     - (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];
    }
    
    后台会话允许您在应用程序未运行时在后台执行内容的上载和下载。您可以通过调用NSURLSessionConfiguration类上的backgroundSessionConfiguration:方法来创建后台会话配置

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

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


    谢谢。。我要试试这个。。但是第二个案例呢?在iOS中,即使用户从app switcher退出应用程序,我也可以下载内容吗?是的。。我在《苹果医生》上读到了这篇文章。。但即使用户退出应用程序,安卓系统中的下载也会发生。我不知道为什么IOS中没有此功能那么在发送远程通知时我应该下载内容吗?因为如果用户退出应用程序,将始终调用此代理。。这是正确的做法吗?@Medha如果你设置了后台NSURLSession任务,下载由操作系统处理,而不是你的应用程序。你的应用程序可以终止/崩溃,但下载不会停止。我尝试了代码并浏览了其他有关NSURLSession的网站。它正在工作。但我还有一个问题。如果应用程序处于后台,请说2天。。如果我想在一天后下载一些东西,我应该怎么做?我是说如何再次触发NSURLSession恢复代码?