Rags93
Rags93

Reputation: 1474

iOS MKMapItem's openInMaps(launchOptions:) does not trigger viewWillDisappear

I am opening from my ViewController the Maps.app via openInMaps(launchOptions:) and it does not trigger any lifecycle method when leaving or going back to the app.

Example Project: https://github.com/raphaelseher/OpenInMaps

Example Code:

let placeMark = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 46.6413035, longitude: 14.2425444))
let mapItem = MKMapItem(placemark: placeMark)
mapItem.name = "Klagenfurt"
mapItem.openInMaps(launchOptions: nil)

Someone able to explain me why this is the behaviour?

Upvotes: 1

Views: 350

Answers (1)

Mats
Mats

Reputation: 8618

It is because your app does not change its visible view controller. So the view controller life cycle events are not triggered.

However, the app life life cycle events are triggered. They can be observed by registering with the default notification center. All available events can be found in the documentation for UIApplication.

You can register an observer with the following code:

NotificationCenter.default.addObserver(self, 
                                       selector: #selector(self.applicationDidResignActive),
                                       name: Notification.Name.UIApplicationWillResignActive,
                                       object: nil)

And also add the method:

func applicationDidResignActive(notification: NSNotification) {     
    // handle event
}

Upvotes: 1

Related Questions