I'm trying to code my app so that when someone clicks on the export button, it brings up a sheet to pick 'text message' to share that single coredata item (in this case a movie with title, genre, and service attributes which are in a JSONserialization) to someone else. It creates a file using a file extension of .wlmv which associates to my app so that when the receiver gets the message, it will automatically open in the app (or if they don't have the app, send to the app page in the App Store). However, I've setup the export function, but the share sheet won't even show giving the error of '[ShareSheet] connection invalidated.' Maybe im going about this in the wrong way but any help is appreciated.
Button calling the function
Button(action: { exportMovieToWLMV(movie: movie) }) {
VStack {
Image(systemName: "square.and.arrow.up")
.font(.system(size:16))
.fontWeight(.heavy)
.foregroundColor(.white)
Text("Share".uppercased())
.font(.system(size:8))
.fontWeight(.heavy)
.foregroundColor(.white)
}
}
Function being called to export the movie item:
func exportMovieToWLMV(movie: Movies) {
let fileName = "movie.wlmv"
let fileManager = FileManager.default
guard let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
let fileURL = documentDirectory.appendingPathComponent(fileName)
do {
let movieData = ["title": movie.title ?? "",
"genre": movie.genre ?? "",
"service": movie.service ?? ""]
let jsonData = try JSONSerialization.data(withJSONObject: movieData, options: .prettyPrinted)
try jsonData.write(to: fileURL)
let av = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
if let rootViewController = UIApplication.shared.windows.first?.rootViewController {
rootViewController.present(av, animated: true, completion: nil)
} else {
print("Error presenting share sheet: unable to get root view controller")
}
print("Exporting Movie....")
} catch {
print("Error exporting movie: \(error.localizedDescription)")
}
}