当app未在后台运行时,使用startmonitoringsignificantlocation更改跟踪设备位置

时间:2013-10-22 11:24:55

标签: iphone ios objective-c cllocationmanager

即使应用未在后台运行,我也希望跟踪iOS设备的位置。我的主要目的是获取设备位置并使用Web服务调用将其发送到服务器。为此,我遵循了本教程http://www.mindsizzlers.com/2011/07/ios-background-location/,但我无法实现同样的目标。

现在,我没有向服务器发送坐标,而是尝试在文本文件中记录位置“但是当我的应用程序没有在后台运行时我无法实现”而且我有一直在使用以下代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    if (!locationManager) {
        locationManager = [[CLLocationManager alloc]init];
    }

    [locationManager setDelegate:self];
//
//    [locationManager startMonitoringSignificantLocationChanges];
//

    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey] ) {
        [locationManager startMonitoringSignificantLocationChanges];
    }

    else{
        [locationManager startUpdatingLocation];

    }
}

-

- (void)applicationWillTerminate:(UIApplication *)application {
    [locationManager startMonitoringSignificantLocationChanges];
}

-

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    [locationManager startMonitoringSignificantLocationChanges];

}

-

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;

        NSLog(@"is in background");
    }

    // Handle location updates as normal, code omitted for brevity.
    // The omitted code should determine whether to reject the location update for being too
    // old, too close to the previous one, too inaccurate and so forth according to your own
    // application design.

    if (isInBackground)
    {
        [self sendBackgroundLocationToServer:[locations lastObject]];
    }
    else
    {
        // ...
    }
}

-

- (void) log:(NSString*)msg
{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    NSString * logMessage = [NSString stringWithFormat:@"%@ %@", [formatter stringFromDate:[NSDate date]], msg];

    NSString * fileName = [self locationPath];
    FILE * f = fopen([fileName cString], "at");
    fprintf(f, "%s\n", [logMessage cString]);
    fclose (f);
}

-

- (NSString*)locationPath
{
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    return [path stringByAppendingPathComponent:@"locationPath.txt"];
}

-

-(void) sendBackgroundLocationToServer:(CLLocation *)location
{
    // REMEMBER. We are running in the background if this is being executed.
    // We can't assume normal network access.
    // bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier

    // Note that the expiration handler block simply ends the task. It is important that we always
    // end tasks that we have started.

    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

                  [[UIApplication sharedApplication] endBackgroundTask:bgTask];
                   }];

                    [self log:[NSString stringWithFormat:@"Background location %.06f %.06f %@" , location.coordinate.latitude, location.coordinate.longitude, location.timestamp]];

                  // ANY CODE WE PUT HERE IS OUR BACKGROUND TASK

                  // For example, I can do a series of SYNCHRONOUS network methods (we're in the background, there is
                  // no UI to block so synchronous is the correct approach here).

                  // ...

                  // AFTER ALL THE UPDATES, close the task

                  if (bgTask != UIBackgroundTaskInvalid)
                  {
                      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
                       bgTask = UIBackgroundTaskInvalid;
                  }
}

-

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

-

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

//    [locationManager stopMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];

}

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

您应该将位置背景模式添加到info.plist。