2011-08-18 35 views
0

我想在后台发布数据,并且我希望从常规间隔中接收响应,以便我尝试使用CLLocation Manager。但didUpdateToLocation方法只调用一次。 请推荐我! 进入这里位置背景 - 网络连接

- (void)applicationDidEnterBackgroundUIApplication *)application 
    { 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m 
    [locationManager startUpdatingLocation]; 
    UIApplication* app = [UIApplication sharedApplication]; 

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_ QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    //Do the work associated with the task 
    // code for Posting Data Here 
    NSLog(@"backgroundTimeRemaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); 
    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
    }); 

    } 
    - (void)locationManagerCLLocationManager *)manager 
     didUpdateToLocationCLLocation *)newLocation 
     fromLocationCLLocation *)oldLocation 
    { 

     NSLog(@"Location Change"); 
    } 
    This Method Call Only Once. 
+0

有你在身边有些感动?我从经验中知道,即使使用kCLDistanceFilterNone,您可能需要移动得相当远(有时超过30米)才能实际发送更新。 –

+0

根据你的位置更新locationManager.distanceFilter值应该是什么。 – Vijay

+0

这是一个很好的价值。但是,根据您的位置,手机可能无法很好地修复您的位置。因此,即使每当它移动一米时它都会通知您,但它可能不知道它已经移动了一米。 –

回答

0

代码确保你是不是在模拟器测试此。 你没有在iPhone模拟器上获得位置更新。

+0

我在设备上测试这个,但方法只调用一次 – Vijay

0

为了在后台运行位置服务,您需要修改plist,使UIBackgroundModes键包含位置。

+0

我allready修改我的应用程序plist作为所需的背景模式:应用程序注册的位置更新 – Vijay

+0

我不知道如果你需要这个或没有,但你的plist还包含两个*所需的设备功能下的位置服务*和* GPS *? –

+0

感谢David V对你的回复 – Vijay

0

通话应该是

[locationManager startMonitoringSignificantLocationChanges]; 
+0

我也称这种方法,但我的问题没有解决 – Vijay