2013-10-22 60 views
0

即使應用程序未在後臺運行,我也想跟蹤iOS設備的位置。我的主要目的是要獲取設備位置並使用Web服務調用將其發送到服務器。爲此,我遵循本教程http://www.mindsizzlers.com/2011/07/ios-background-location/,但我無法達到相同的效果。當應用程序未在後臺運行時使用startmonitoringsignficantlocation更改跟蹤設備位置

現在,而不是發送座標到服務器我試圖登錄的位置在一個文本文件「但我不能夠實現它,當我的應用程序沒有在後臺運行」,我一直在使用下面的代碼對於相同的。

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

} 

任何幫助將非常感激。

謝謝

+0

我不知道爲什麼有人投了我的問題,但我會要求如果問題中有任何問題,那麼請提及它,以便下次用戶可以提出更好的問題。 –

回答

0

您應該將位置背景模式添加到您的info.plist。

相關問題