回答編集履歴

1

コメントへの回答追記

2018/07/31 05:17

投稿

razuma
razuma

スコア1313

test CHANGED
@@ -5,3 +5,131 @@
5
5
 
6
6
 
7
7
  [【Swift】UIDocumentInteractionControllerで指定したアプリが開かないとき](http://6rats.blog62.fc2.com/blog-entry-206.html)
8
+
9
+
10
+
11
+ コメントへの回答追記:
12
+
13
+ UIDocumentInteractionControllerのurlの中身がおかしいのかもしれません。
14
+
15
+ 保存できたコードを以下に貼りますので試してみてください。
16
+
17
+ ```
18
+
19
+ class ViewController: UIViewController {
20
+
21
+
22
+
23
+ private var tapData: [[String]] = [["11", "22"]]
24
+
25
+
26
+
27
+ var documentInteraction: UIDocumentInteractionController!
28
+
29
+
30
+
31
+ override func viewDidLoad() {
32
+
33
+ super.viewDidLoad()
34
+
35
+
36
+
37
+ }
38
+
39
+
40
+
41
+ @IBAction func sendFile(_ sender: Any) {
42
+
43
+ createFile(fileArrData: tapData)
44
+
45
+ }
46
+
47
+
48
+
49
+ func createFile(fileArrData : [[String]]){
50
+
51
+
52
+
53
+ var fileStrData:String = ""
54
+
55
+ let fileName = "tapdata4.7inch.csv"
56
+
57
+
58
+
59
+ //StringのCSV用データを準備
60
+
61
+ for singleArray in fileArrData{
62
+
63
+ for singleString in singleArray{
64
+
65
+ fileStrData += "\"" + singleString + "\""
66
+
67
+ if singleString != singleArray[singleArray.count-1]{
68
+
69
+ fileStrData += ","
70
+
71
+ }
72
+
73
+ }
74
+
75
+ fileStrData += "\n"
76
+
77
+ }
78
+
79
+ print(fileStrData)
80
+
81
+
82
+
83
+ // DocumentディレクトリのfileURLを取得
84
+
85
+ let documentDirectoryFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!
86
+
87
+
88
+
89
+ // ディレクトリのパスにファイル名をつなげてファイルのフルパスを作る
90
+
91
+ let FilePath = documentDirectoryFileURL.appendingPathComponent(fileName)
92
+
93
+
94
+
95
+ print("書き込むファイルのパス: (FilePath)")
96
+
97
+
98
+
99
+ do {
100
+
101
+ try fileStrData.write(to: FilePath, atomically: true, encoding: String.Encoding.utf8)
102
+
103
+ } catch let error as NSError {
104
+
105
+ print("failed to write: (error)")
106
+
107
+ }
108
+
109
+
110
+
111
+ documentInteraction = UIDocumentInteractionController()
112
+
113
+ documentInteraction.url = FilePath
114
+
115
+
116
+
117
+ if !(documentInteraction?.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true))!
118
+
119
+ {
120
+
121
+ // 送信できるアプリが見つからなかった時の処理
122
+
123
+ let alert = UIAlertController(title: "送信失敗", message: "ファイルを送れるアプリが見つかりません", preferredStyle: .alert)
124
+
125
+ alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
126
+
127
+ self.present(alert, animated: true, completion: nil)
128
+
129
+ }
130
+
131
+ }
132
+
133
+ }
134
+
135
+ ```