尝试与活动视图控制器共享文件中的json字符串

时间:2018-06-05 15:07:05

标签: swift uiactivityviewcontroller

iOS 11,Swift 4 试图使用活动视图控制器来共享我刚刚创建的json字符串,我想我几乎就在那里,但很难用控制器附加文件。我收到了这段代码。

    let encoder = JSONEncoder()
    if let jsonData = try? encoder.encode(w2GA) {
        if let jsonString = String(data: jsonData, encoding: .utf8) {
            let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            let file2ShareURL = documentsDirectoryURL.appendingPathComponent("blah.json")
            print(jsonString)
            do {
                let encodedData = try? JSONEncoder().encode(jsonString)
                try encodedData?.write(to: file2ShareURL)
                    let activityViewController = UIActivityViewController(activityItems: [file2ShareURL], applicationActivities: nil)
                    activityViewController.popoverPresentationController?.sourceView = self.view
                    self.present(activityViewController, animated: true, completion: nil)

            } catch {
                print(error)
            }
        }
    }

我收到错误消息:

  

file:///var/mobile/Containers/Data/Application/FD6E9F52-405F-4E66-927F-DCB7EDB0BF25/Documents/blah.json

     

附件错误确认URL可读

很确定文件在那里?这可能是某种种族危险吗?如果我用jsonString替换URL它可以工作!所以我得到的jsonString基本上就是这样。

  

[{" IMAGEURL":" HTTP://""纬度":46.819945794990176,"名称":&#34 ;线索1","经度":8.2581710034376599,"提示":"提示"},{" imageURL":&#34 ; http://","纬度":47.433033706679716,"名称":"线索2","经度":8.8540648624925371 "提示":"提示"},{" IMAGEURL":" HTTP://""纬度&#34 ;:46.785125219263776,"名称":"线索""经度":9.6534346734197598,"提示":"提示" }]

2 个答案:

答案 0 :(得分:0)

您需要将其另存为文件吗?

当然,最简单的方法是将JSON字符串作为变量传递?

示例:

let activityViewController = UIActivityViewController(activityItems: [jsonString], applicationActivities: nil)

修改

我刚刚注意到你已经尝试了这个并且它有效。道歉。

在您尝试将网址传递给JSONEncoder

之前,确保UIActivityViewController没有抛出异常且文件存在

您还可以尝试使用here

记录的UIActivityItemProvider课程

答案 1 :(得分:0)

好的,找到答案当然很明显!该文件尚未创建!

我做了两个更改,首先我在主线程下运行整个代码块,然后我更改了写出json字符串的行,在早期版本中我编码了两次!

     DispatchQueue.main.async {
        let encoder = JSONEncoder()
        if let jsonData = try? encoder.encode(w2GA) {
            if let jsonString = String(data: jsonData, encoding: .utf8) {
                var documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                let file2ShareURL = documentsDirectoryURL.appendingPathComponent("blah.json")
                do {
                    try jsonString.write(to: file2ShareURL, atomically: false, encoding: .utf8)
                } catch {
                    print(error)
                }

                do {
                    let _ = try Data(contentsOf: file2ShareURL)
                    let activityViewController = UIActivityViewController(activityItems: [file2ShareURL], applicationActivities: nil)
                    activityViewController.popoverPresentationController?.sourceView = self.view
                    self.present(activityViewController, animated: true, completion: nil)
                } catch {
                    print(error)
                }

        }
    }
}