I would like to present a full screen ViewController without any knowledge about the current ViewController hierarchy. My current solution to find the ViewController on which I can always present my full screen ViewController is the following:
+ (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
The background why I need this: our app uses a lot of
[[UIApplication sharedApplication]openURL:[NSURL urlWithString:@"http://www.someurl.com"]];
calls which open Safari externally. Apple started to reject this, because they think it's detrimental to the user experience, and we should use SFSafariViewController instead. But unlike openURL this requires a reference to a ViewController on which we can present SFSafariViewController. I don't want to change the code to acquire the appropriate ViewController at dozens of places, instead a universal method would be nice which gets the appropriate ViewController. The code I listed works every single time in our app, but I am unsure if it's really universal.