There are two options:
You can use
MKDirections to get directions right in your app, without ever going to the Maps app.
You can also pass it off to the Maps app (like your above example) so that the user can then enjoy the full real-time navigation (see traffic, etc.).
But you don’t use the “open in Maps” API to return directions within your app. Use
MKDirections for that.
In your code snippet, you are doing:
let view = MKAnnotationView()
That creates an blank annotation view (with no annotation), so obviously if you try to get the annotation for it, it doesn't have one. You presumably want to either
- save the original annotation that you added to your map view and use that;
- respond to one of the
MKMapViewDelegate methods and use the
annotation for that annotation view on your map view; or
- go back to your original data source from which you originally created the annotation and create a new annotation (not annotation view) from that.
But don't try to create a new, blank annotation view and try to retrieve the annotation view for it, because it won't have one.