Is this a bug with MKMapKitDelegate mapView:didUpdateUserLocation?
My opinion is that it's a bug.
How can the map view say user location has been updated when the user hasn't even granted permission?
The workaround I use is to check if userLocation.location
is nil
:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (userLocation.location == nil)
return;
//do something with userLocation...
}
iOS MapKit crashing through didUpdateUserLocation - baffled
The problem usually occurs when the nib for the Map is closed and it
has dealloced the view.
[mapView setRegion:region animated:NO];
Well isn't that your problem then?
This is why we have dependency inversion patterns like Observer/Listener.
Instead of fiddling around with the view directly with your CLLocationManager delegate, use NSNotification to alert subscribing views (such as your MapView) that it should update. If the MapView has been dealloced it won't do anything.
How to check validity of CLLocation in iOS
As Abizern's comment implies, you shouldn't assume the location will be ready to use immediately after setting showsUserLocation
to YES
.
When the location is available, the map view will call its delegate method didUpdateUserLocation
.
(If getting the location fails, the didFailToLocateUserWithError
delegate method will be called.)
Outside of the didUpdateUserLocation
method, here are a couple of ways to check if the location is ok to use:
- Check if
userLocation.location
isnil
. If it is, the location hasn't been obtained yet, it failed, orshowsUserLocation
isNO
. - If the location is not
nil
, then you can look at thecoordinate
property and that can be checked (if you think it might not be valid) using theCLLocationCoordinate2DIsValid
function. Note however that coordinate 0,0 is valid (this generally happens when location is nil).
I have noticed that even in the didUpdateUserLocation
delegate method, userLocation.location
can be nil
.
This seems to happen when running an app for the first time and after setting showsUserLocation
to YES
. At that point, iOS prompts the user with "Allow app to use your location?" while at the same time the delegate is called (even though the user hasn't yet responded to the prompt and the location hasn't been determined).
So at the top of that delegate method, you should also check if userLocation.location
is nil
.
By the way, in your code you may want to set the delegate before setting showsUserLocation
.
File bug for iPhone SDK
The only thing in your control is actually #1, that is you can file bugs. You can do this through http://developer.apple.com/bugreporter/ for all of Apple's products including the SDK IIRC.
Your other two points are not so easy since you can only see your bugs. You will just have to trust that when Apple runs them through triage that thy properly flag duplicates. Some people do include the Radar url for their bug when positing n to the mailing lists and the like. That URL only works for Apple employees, but the hope is that if you make it easy to find your bug it might get fixed sooner.
File bug for iPhone SDK
The only thing in your control is actually #1, that is you can file bugs. You can do this through http://developer.apple.com/bugreporter/ for all of Apple's products including the SDK IIRC.
Your other two points are not so easy since you can only see your bugs. You will just have to trust that when Apple runs them through triage that thy properly flag duplicates. Some people do include the Radar url for their bug when positing n to the mailing lists and the like. That URL only works for Apple employees, but the hope is that if you make it easy to find your bug it might get fixed sooner.
Related Topics
How to Switch to Speaker Output When Bluetooth Headsets Are Connected
Private VS. Fileprivate on Declaring Global Variables/Consts in Swift3
Replace iOS App Emoji with Twitter Open Source Twemoji
How to Use Avcapturephotooutput to Capture Photo Swift + Xcode
How to Have Firebase Automatically Delete Values Older Than 30 Minutes
Rails: Redirect_To 'Myapp://' to Call iOS App from Mobile Safari
Ignoring the Dynamic Type in iOS: Accessibility
iOS How to Use Uiapplication Launchapplicationwithidentifier Which Is in Private APIs
Drawing Rounded Rect in Core Graphics
Recording from Remoteio: Resulting .Caf Is Pitch Shifted Slower + Distorted
How to Play Multiple Audio Files Simultaneously
Creating an Image Out of the iOS Surface and Saving It
How to Emulate Nfc Cards in iOS 13
Swift - Add Gesture Recognizer to Object in Table Cell
Game Center Login Dialog Not Shown Again After Cancelling It for the First Time (Ios7)
Uisplitviewcontroller on iPad with Storyboards