iOS应用程序进入后台时下载数据

时间:2018-01-31 07:06:31

标签: ios objective-c background-process nsurlsession

我有一个应用程序,它在第一次安装时会下载大量数据(主要是图像和文档文件)。目前,我只能在HUD中显示进度。但我希望我可以以某种方式允许在应用程序进入后台(或设备被锁定)时下载数据。由于该应用针对运行iOS 7.0及更高版本的设备,我使用NSURLSession下载数据。我在Stackoverflow上看过各种各样的线程以及教程here。即使根据教程对我的应用程序进行了更改,我的应用程序也无法继续下载。我在iPad上测试过它。当应用程序被发送到后台(或锁定)时,下载暂停并在应用程序到达前台时恢复。

我不确定我的方法是错误的还是我的实施存在缺陷。欢迎任何帮助/建议。

  

应用程序的流程如下:'LoginViewController'调用内部方法downloadData,该方法具有执行下载任务的SaveProjectData类对象。

LoginViewController.m

@implementation LoginViewController
- (void)viewDidLoad {
}
- (IBAction)sumitButtonDidClick:(id)sender {

    if ([self checkNetworkConnection] ==NotReachable) {

        UIAlertView * alertView=[[UIAlertView alloc] initWithTitle:@"Network Error" message:@"No internet Connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alertView show];

    } else {

       MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
        hud.label.text = NSLocalizedString(@"Downloading...", @"HUD loading title");

         dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{

        double delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

            if ([self loginStatus]) {

                NSString * userName = self.user_nameTxtField.text;
                 CallProjectAPI * callProjectAPIinst = [[CallProjectAPI alloc] init];

                NSString * appStatus = [[NSUserDefaults standardUserDefaults] objectForKey:@"appStatus"];
                if ([appStatus isEqualToString:@"N"]) {
                       [callProjectAPIinst dataFromJSONFile];
                      [callProjectAPIinst saveImageName];

                } else {

                     [self doSomeWorkWithProgress];
                    [callProjectAPIinst callAPI];
                     [self doSomeWorkWithProgress];
                    [self downloadData]; 
                }


                hud.label.text = NSLocalizedString(@"Complete!", @"HUD completed title");


                porjectVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProjectViewController"];
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [self presentViewController:porjectVC animated:YES completion:nil];
                     [hud hideAnimated:YES];
                  });
                [[NSUserDefaults standardUserDefaults] setObject:userName forKey:@"userName"];

            } else {

                dispatch_async(dispatch_get_main_queue(), ^{
                     [hud hideAnimated:YES];
                    UIAlertView * alert=[[UIAlertView alloc]  initWithTitle:@"Alert" message:@"User Name or Password is wrong." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
                    [alert show];

                });

            }


         });

    }
}
-(void)downloadData{


    if ([self checkNetworkConnection] ==NotReachable) {

        UIAlertView * alertView=[[UIAlertView alloc] initWithTitle:@"Network Error" message:@"No internet Connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alertView show];

    } else {
        NSString * status =[[NSUserDefaults standardUserDefaults] objectForKey:@"DownloadData"];
        if (status == nil) {




                SaveProjectData * saveProjectDataInst = [[SaveProjectData alloc] init];

                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveAboutImage];
                [self doSomeWorkWithProgress];
                [saveProjectDataInst saveConstructionUpdateImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveLogImages];
                [saveProjectDataInst saveSmallImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveFloorPlanImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveUnitPlanImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveMasterPlanImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveBrochurs];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveGalleryImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveAmenitiesImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveVideos];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveMapImage];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveBannerImage];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst savewalkthrough];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveFlatImages];
                 [self doSomeWorkWithProgress];
                [saveProjectDataInst saveEventImages];
                 [self doSomeWorkWithProgress];

                [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"DownloadData"];
               [saveProjectDataInst getUpdatedListForEachProject];
        }
    }
}
- (void)doSomeWorkWithProgress {

    progress += 0.049f;
    dispatch_async(dispatch_get_main_queue(), ^{
        // Instead we could have also passed a reference to the HUD
        // to the HUD to myProgressTask as a method parameter.
        [MBProgressHUD HUDForView:self.view].progress = progress;
    });


}
@end
  

另一种方法可能是提供一个用户点击并下载数据的按钮,而用户仍然可以继续使用   其他用途的设备。关于如何实现的任何指示   它吗

2 个答案:

答案 0 :(得分:0)

如果您的应用处于后台,您不应该依赖下载内容,因为您不知道何时会因内存压力或其他原因而被杀死

这是关于它的Apple文档

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html#//apple_ref/doc/uid/TP40007072-CH2-SW7

答案 1 :(得分:0)

@HimanshuMahajan给出了here这个问题(类似问题)的答案 我正在添加这个答案并发布一个适合我的解决方案,并在iPad上成功测试了它。

1)在ViewController的头文件中使用以下行

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;

2)在ViewDidLoad中分配UIBackgroundTaskIdentifier,如:

self.backgroundTask = UIBackgroundTaskInvalid;

3)使用以下代码行,这里我只是保留beginBackgroundTaskWithExpirationHandler:块内的getDataFromServer方法

self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }];

    /* Here your downloading Code, let say getDataFromServer method */

    [self getDataFromServer]; // Its dummy method

    /* Your downloading Code End Here */

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    });

4)如果要检查后台下载数据的剩余时间,请在applicationDidEnterBackground中包含以下行:AppDelegate的(UIApplication *)应用程序委托方法:

NSLog(@"Background time remaining = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);
  

添加答案:

5)在applicationDidEnterBackground:(UIApplication *)application方法中添加以下代码,以允许无时间限制的后台执行

UIApplication *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];

希望有人发现答案有用!