Created
November 27, 2019 15:57
-
-
Save anka/8ddcea6fafa30e69506794c3ecd0afee to your computer and use it in GitHub Desktop.
iOS share method for sharing a file through Flutter's platform channel using UIActivityViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func shareFile(arguments:Any?) -> Void { | |
let argsMap = arguments as! NSDictionary | |
let subject = argsMap.value(forKey: "subject") as? String | |
let filepath = argsMap.value(forKey: "filepath") as? String | |
let x = argsMap.value(forKey: "x") as? NSNumber | |
let y = argsMap.value(forKey: "y") as? NSNumber | |
let width = argsMap.value(forKey: "width") as? NSNumber | |
let height = argsMap.value(forKey: "height") as? NSNumber | |
// Add shareable activity items | |
var activityItems:[Any] = []; | |
if let path = filepath { | |
activityItems.append(NSURL(fileURLWithPath: path)) | |
} | |
if let title = subject, !title.isEmpty { | |
activityItems.append(title); | |
} | |
// Set up activity view controller | |
let controller = UIApplication.shared.keyWindow!.rootViewController as! FlutterViewController | |
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil) | |
activityViewController.popoverPresentationController?.sourceView = controller.view | |
// Present the view controller indicating the share action origin | |
if let posX = x, let posY = y, let width = width, let height = height { | |
let sourceRect = CGRect(x: posX.doubleValue, y: posY.doubleValue, width: width.doubleValue, height: height.doubleValue) | |
activityViewController.popoverPresentationController?.sourceRect = sourceRect | |
} | |
controller.show(activityViewController, sender: self) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment