swiftxcodeswiftuirootviewcontrollerios-sharesheet

Button Tap - ShareSheet rootViewController Issue


I'm having issues when tapping buttons that either open the share sheet or email.

Behavior - Does not work when app is launched after install. Works fine after restarting app.

UIApplication.shared.windows.first?.rootViewController?.present(shareController, animated: true, completion: nil)

Behavior - Works fine when app is launched after install. Does not work after restarting app.

UIApplication.shared.windows.first?.rootViewController!.presentedViewController?.present(shareController, animated: true, completion: nil)

Log details:

Attempt to present which is already presenting.

ShareSheet connection invalidated.

I need this to work in both scenarios.


Solution

  • This worked for me earlier today:

    Extension:

    extension UIApplication {
        
    static func TopPresentedViewController() -> UIViewController? {
        
        guard let rootViewController = UIApplication.shared
                .connectedScenes.lazy
                .compactMap({ $0.activationState == .foregroundActive ? ($0 as? UIWindowScene) : nil })
                .first(where: { $0.keyWindow != nil })?
                .keyWindow?
                .rootViewController
        else {
            return nil
        }
        
        var topController = rootViewController
        
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        
        return topController
       } 
    }
    

    To present:

    UIApplication.TopPresentedViewController?.present(activityViewController, animated: true, completion: nil)