startMonitoringForRegion并不总是有效

时间:2012-05-23 15:22:36

标签: iphone objective-c ios ios5 location

我有一个应用程序,我需要使用StartMonitoringForRegion。 当我在应用程序中放入以下代码时,didFinishLaunchingWithOptions就可以了。

[loc startUpdatingLocation];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(21.456372,39.287972);
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:200 identifier:@"C"];
[self.locationManager startMonitoringForRegion:region desiredAccuracy:50];

但是当我在另一个视图中使用相同的代码时,它不起作用!! 我尝试使用以下代码在主线程上执行代码:

if (![NSThread isMainThread])
{
    [self performSelectorOnMainThread:@selector(MonitorRegion:) withObject:pr waitUntilDone:NO];
    return;
}

但它仍然不起作用!

为什么代码在didFinishLaunchingWithOptions中工作而不在另一个视图中工作!

是否存在区域监视器可能无法正常工作的情况?

1 个答案:

答案 0 :(得分:2)

在搜索和不同的试验后,我想出了如何解决它。

只是该视图必须始终可见并且一直在运行,以便使didEnterRegion和didExitRegion正常工作。

我将所有内容都移到appDelegate,现在它正在运行。