StopUpdatingLocation方法不适用于iOS5

Ank*_*nki 4 iphone cocoa-touch objective-c ios4 ios5

我正在研究地图应用,

我尝试使用[locationManager stopUpdatingLocation];方法来停止Location服务.

它似乎在iOS4.3中运行良好,但在iOS5中,它无法正常工作.

请任何人建议我如何在iOS5中停止位置服务?

P.J*_*P.J 13

有时会发生位置管理员即使在之后也不会停止 stopUpdationLocations

您需要为该集释放您的位置管理员

locationManager = nil;
Run Code Online (Sandbox Code Playgroud)

这应该写在之后

[locationManager stopUpdatingLocation];

另外,在将其设置为nil之后,请注意不要调用locationManager对象,否则您的应用程序将崩溃

如果它仍然没有停止位置服务,那么有解决办法,如果你不想在将来使用它,只需使用BOOL变量.

希望这对你有所帮助!


Par*_*att 5

您可以从以下两个链接获得帮助:

LINK-1:

http://iphonedevsdk.com/forum/iphone-sdk-development/2299-cllocationmanager-stopupdatinglocation-not-working.html

请参阅上面链接中的site_reg的答案:

我遇到了同样的问题,似乎可以通过在.m文件中声明一个静态BOOL并在输入didUpdateToLocation方法时检查它来解决.

@implementation MyFile
@synthesize MySynth;
static BOOL haveAlreadyReceivedCoordinates = NO;
Run Code Online (Sandbox Code Playgroud)

然后在你的didUpdateToLocation方法中:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    if(haveAlreadyReceivedCoordinates) {
        return;
    }
    haveAlreadyReceivedCoordinates = YES;
    ...
}
Run Code Online (Sandbox Code Playgroud)

这在技术上不会使它停止接收更新,但它将忽略第一个之后的任何更新.如果您的应用依赖于只获取一个位置更新来完成其工作,这应该会有所帮助.

编辑-1:

一旦完成此操作,您可以在线locationManager.delegate = nil;后添加[locationManager stopUpdatingLocation];.

LINK-2:

为什么self.locationManager stopUpdatingLocation不会停止位置更新

请参阅以上链接中的jnic的答案:

相反的startMonitoringSignificantLocationChanges是不 stopUpdatingLocation,它是 stopMonitoringSignificantLocationChanges.

你可能要替换 startMonitoringSignificantLocationChangesstartUpdatingLocation的更多的定期更新的缘故,除非你有只监控显著位置变化的具体原因.

查看CLLocation文档 以获取更多详细信息.

我已添加链接中的答案,以确保即使将来提供的链接断开,答案也很有用.