问题
I have developed an app targeted for iOS 4.3. It worked fine. But since I migrated to iOS 5.0, the app has started showing strange behavior. The location manager is not showing updates on change in position. Has anyone encountered this kind of problem?
Thanks for any help.
回答1:
I had my fare share with CLLocation manager weird updating issues when moving from iOS 4.x to 5:
Have you subclassed the CLLocationManager object ?
problem : In that case sometime the delegate methods sometime don't get called (ex. didUpdateLocation).
fix: don't subclass CLLocationManager
- Is the CLLocationManager instanced from a Singleton ?
- problem: using the CLLocationManager object as a property of a singleton object worked fine in 4.x but was inconsistent in iOS5
- fix:move the object to a non-singleton object
- If either of the above helps , or you've tried it and it failed
- problem: for some reason the CLLocationManager still doesn't run from the main thread and so you experience the strange behavior.
- fix#1: create the CLLocationManager object as a property in the appDelegate without subclassing it . (this solution proved to work best , even on jailbroken iphones) . you can still use a dynamic property pointing to it's instance from the singleton if you design is similar to case 2.
- fix#2: a more dirty solution is making sure it runs from the main runloop either using performSelectorOnMainThread or some other way
Hope this helps :)
回答2:
The way iOS 5 handles memory management could be one way. If you are declaring a variable in your .h file and not referencing it correctly or implicitly [self.myVar doSomething]; it could be dumping out on you without you knowing it. It will still compile fine, but will mainly be ignored and passed over.
Not 100% without seeing code, but I have noticed this issue as well with some of our company's older programs when iOS 5 came calling. Good luck.
回答3:
The key classes contained within the Core Location framework are CLLocationManager and CLLocation. An instance of the CLLocationManager class is created within the application and a property set to indicate the level of location accuracy that is required by the application. The location manager is then instructed to start tracking location information:
CLLocationManager *locationMgr =
[[CLLocationManager alloc] init];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.delegate = self;
[locationMgr startUpdatingLocation];
With each location update, an application delegate method named didUpdateToLocation is called by the location manager and passed information about the current location. The above code also, therefore, assigns the current class as the location manager’s delegate.
Also in case you were interested I got this information from here and it is made specifically for iOS 5: http://www.techotopia.com/index.php/Getting_iPhone_Location_Information_using_the_iOS_5_Core_Location_Framework
回答4:
Ran into an intermittent problem in an app where the location was cached. Sometimes location services takes longer than expected to return a location, and instead it returns the last cached location. See this iOS documentation example that uses a howRecent
variable to filter out locations that are likely cached.
Just in case Apple changes their URL, the relevant delegate method is listed here:
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// If it's a relatively recent event, turn off updates to save power
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0)
{
NSLog(@"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
}
// else skip the event and process the next one.
}
This effectively ignores any locations older than 15 seconds. We set the interval to 5.0
in our app, but you'll have to experiment to see what works well for you.
来源:https://stackoverflow.com/questions/7857450/gps-app-stopped-working-on-ios-5-location-manager-not-updating