Skip to content

Instantly share code, notes, and snippets.

@ponnamkarthik
Created October 1, 2022 11:20
Show Gist options
  • Save ponnamkarthik/db50dd16ff8d1fa0643b74172caa2338 to your computer and use it in GitHub Desktop.
Save ponnamkarthik/db50dd16ff8d1fa0643b74172caa2338 to your computer and use it in GitHub Desktop.
ios UIApplication extension function to get the UIViewController in flutter plugin
var sharedItems : Array<NSObject> = Array()
sharedItems.append((message as NSObject?)!)
let activityViewController = UIActivityViewController(activityItems: sharedItems, applicationActivities: nil)
activityViewController.setValue("Share", forKeyPath: "subject");
DispatchQueue.main.async {
UIApplication.topViewController()?.present(activityViewController, animated: true, completion: nil)
}
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment