0

i need to get and send location to server even if application is terminated, i found solution how to get location, but how i can send it to server - i cant found. I think i can send it with background task, but how it together work, i can't imagine, i try with this code indidFinishLaunchingWithOptions:

if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
        // This "afterResume" flag is just to show that he receiving location updates
        // are actually from the key "UIApplicationLaunchOptionsLocationKey"

        self.shareModel.afterResume = YES;

        [self.shareModel startMonitoringLocation];
        [self.shareModel addResumeLocationToPList];


        UIApplication *application = [UIApplication sharedApplication];
        __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
            [application endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }];
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"DOWNLOADTASK"];
            [application endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;

        }];



    }

but than i print NSUserDefaults its empty by this key, what i doing wrong, may be i'm using the wrong background task ??

How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended

https://github.com/voyage11/GettingLocationWhenSuspended

iOS 9 how get locations even if app terminated

Community
  • 1
  • 1
Sergio
  • 319
  • 4
  • 19

2 Answers2

0

According to the apple in the document about the description of the background, any app background task execution time of 10 minutes.After 10 minutes, the app will be iOS forced to hang up. But there are five types of app allows for "unlimited" background running time.

  1. Audio。

  2. Location/GPS。

  3. VoIP。

  4. Newsstand。

  5. Exernal Accessory 。

You can take any app statement for the above five types in order to obtain the background of infinite time, but when you submit the app to the app Store, apple will review your app, once found you "abuse" of the backend API, your app will be rejected. However, in the case of enterprise development, there is no "abuse" problem - enterprise app can through OTA deployment, not through the apple store review. In enterprise deployment, you can be a statement for VoIP app, but the program simply has nothing to do with VoIP, we only for the purpose of the iOS give us infinite background execute permissions.Declaration process is in the app Info. The file to add the following key: In the first place in the file the Required background modes that add the following two: in a App play audio or streams audio/video using AirPlay and App provides Voice over IP services. UIBackgroundModes

voip

I tested the following code:

- (void)backgroundHandler {
    NSLog(@"### -->backgroundinghandler");
    UIApplication*    app = [UIApplicationsharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
 }

Through the test, I have received an "infinite" background execution time.I don't know how long do you think is "infinite", but in this case, the background task to run at least 55 hours, until I lose patience to stop testing. my english is very poor,this blogs are reprinted!

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
BlueWorld
  • 16
  • 2
0

You can't perform any task in suspended or terminated state as per apple's guideline. you can get location updates in background state but can't do it when app is terminated or suspended. Refer this link and this tutorial for background location fetch. hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75