I have an iOS App that uses an UIActivityViewController to share files. It works perfectly on iPad, iPhone and on Mac using (Made for iPad). But when I test the Mac Catalyst app it works perfectly except "Save" and "Copy" are missing from the Share Panel. I think it must be an issue with entitlements and the sandbox. I have tried every folder location available from FileManager.default. If I send the file content as Data rather than a URL that only works on iPad/iPhone while Mac Catalyst share panel shows no options at all.
I tried using url.startAccessingSecurityScopedResource() but that always returned false and made no difference.
I have set the App Sandbox entitlements to Read/Write for User Selected File.
There must be a location where I can save the temporary file and the Share Panel can access it for file Save and Copy actions? Any ideas on the file path? Thanks
Here is the code.
// dataToShare is initialised with the actual data for the file to export,
// before the UIActivityViewController is called
// data is presented to the UIActivityViewController using UIActivityItemSource
var dataToShare = Data()
func proceedToShare(sender: UIViewController, anchorButton: UIView) {
// activityController is a retained var
activityController = UIActivityViewController(activityItems: [self], applicationActivities: nil)
activityControllerpletionWithItemsHandler = {_,_,_,_ in sender.navigationController?.popViewController(animated: true) }
if UIDevice.current.userInterfaceIdiom == .pad {
// also MacCatalyst and madeForIpad
activityController.popoverPresentationController?.sourceView = anchorButton
activityController.modalPresentationStyle = .popover
} else {
activityController.isModalInPresentation = true
}
sender.present(activityController, animated: true)
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return fileContentForExport()
}
func fileContentForExport() -> Any {
if ProcessInfo().isMacCatalystApp {
/* This is called for both MacCatalyst and iOS (designed For iPad apps) */
let directoryURL = FileManager.default.temporaryDirectory
let filename = "ExportFile.txt"
let fileURL = directoryURL.appendingPathComponent(filename))
do {
try dataToShare.write(to: fileURL)
return fileURL
} catch let error { print(error.localizedDescription) }
}
/* iPhone and iPad on iOS handle data directly */
return dataToShare
}