尝试设置将核心数据项导出到文本消息,但不起作用(最终也导入)

问题描述 投票:0回答:0

我正在尝试对我的应用程序进行编码,以便当有人单击导出按钮时,它会弹出一个工作表来选择“文本消息”以共享单个核心数据项(在本例中为具有标题、流派和服务属性的电影在 JSON 序列化中)给其他人。它使用 .wlmv 的文件扩展名创建一个文件,该文件与我的应用程序相关联,以便当接收者收到消息时,它将自动在应用程序中打开(或者如果他们没有该应用程序,则发送到应用商店)。但是,我已经设置了导出功能,但共享表甚至不会显示“[ShareSheet] 连接无效”的错误。也许我以错误的方式解决这个问题,但我们将不胜感激。

调用函数的按钮

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)
    }
}

被调用以导出电影项目的函数:

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)")
    }
}
ios json swiftui core-data export
© www.soinside.com 2019 - 2024. All rights reserved.