0

アプリケーションがバックグラウンド モードのときに、アプリケーションから大きなファイル (ビデオ) をアップロードしたいと考えています。AFNetworking ライブラリを使用しています。アプリケーションは 3 分間実行されていますが、その後はすべてのアクティビティが強制終了されます。

以下のコードは、アプリケーションで使用します。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {} failure:^(AFHTTPRequestOperation *operation, NSError *error) {}];

[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                            long long totalBytesWritten,
                                            long long totalBytesExpectedToWrite) {}];

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{}];

[manager.operationQueue addOperation:operation];
4

2 に答える 2

0

最後に、以下のコードを使用して問題を解決します。以下のタラをapplicationDidEnterBackgroundに入れてください。ファイルのアップロードが完了したら、位置情報の更新とタイマーを停止する必要があります。

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
        if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
            UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

            __block UIBackgroundTaskIdentifier background_task; //Create a task object

            background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
                [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
                background_task = UIBackgroundTaskInvalid; //Set the task to be invalid

                //System will be shutting down the app at any point in time now
            }];

            //Background tasks require you to use asyncrous tasks

            if (videoManager.isUploading)
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    //Perform your tasks that your application requires

                    /*[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
                     background_task = UIBackgroundTaskInvalid; //Invalidate the background_task*/

                    if (self.locManager != nil)
                    {
                        [self.locManager stopUpdatingLocation];
                        [self.locManager stopMonitoringSignificantLocationChanges];
                    }

                    self.locManager = [[CLLocationManager alloc] init];
                    self.locManager.desiredAccuracy = kCLLocationAccuracyKilometer;
                    if (IS_OS_8_OR_LATER)
                    {
                        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
                        {
                            [self.locManager requestAlwaysAuthorization];
                        }
                    }
                    self.locManager.delegate = self;
                    [self.locManager setDistanceFilter:1000];
                    self.locManager.pausesLocationUpdatesAutomatically = NO;
                    [self.locManager startMonitoringSignificantLocationChanges];
                    [self.locManager startUpdatingLocation];
                });

                if (![timer isValid])
                {
                    timer = [NSTimer scheduledTimerWithTimeInterval:60
                                                             target:self
                                                           selector:@selector(changeAccuracy)
                                                           userInfo:nil
                                                            repeats:YES];
                }

            }
            else
            {
                [self.locManager stopUpdatingLocation];
                [self.locManager stopMonitoringSignificantLocationChanges];
                fromBackGround = false;
                self.locManager.activityType = CLActivityTypeAutomotiveNavigation;
                [self.locManager setDesiredAccuracy:kCLLocationAccuracyBest];
                [self.locManager setDistanceFilter:kCLDistanceFilterNone];
                self.locManager.pausesLocationUpdatesAutomatically = NO;


                [self.locManager startUpdatingLocation];
            }
        }
    }

- (void) changeAccuracy{
[self.locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locManager setDistanceFilter:900];}
于 2016-09-21T12:06:16.100 に答える