質問編集履歴
2
問題点を整理し、シンプルな文章に変更しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,142 +1,32 @@
|
|
1
1
|
### 内容
|
2
2
|
|
3
|
+
現在、ビューワーアプリを製作中です。
|
4
|
+
|
3
|
-
|
5
|
+
iCloudに置いたデータを読み込みたいのですが、うまくいかず困っています。
|
4
6
|
|
5
7
|
|
6
8
|
|
7
|
-
|
9
|
+
以下のパスはわかっています。
|
8
10
|
|
9
|
-
|
11
|
+
let filepath = "/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/<ファイル名>"
|
10
|
-
|
11
|
-
選択したファイルのパスを記憶し、次回アプリ起動時に記憶したパスのデータを表示したいと思っています。
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
### 問題
|
16
|
-
|
17
|
-
|
15
|
+
ローカルファイルの読み込みと同じように、以下のように処理を書きましたが、エラーになってしまいます。
|
18
|
-
|
19
|
-
良いやり方をご存じの方がいらっしゃいましたら、ご教示のほどお願い申し上げます。
|
20
|
-
|
21
|
-
### 該当のソースコード
|
22
16
|
|
23
17
|
|
24
18
|
|
25
19
|
```swift
|
26
20
|
|
21
|
+
do{
|
27
22
|
|
28
|
-
|
29
|
-
// アプリ終了時に前に開いたファイルパスを記憶しておきます。
|
30
|
-
|
31
|
-
|
23
|
+
let text = try String( contentsOfFile: filepath, encoding: String.Encoding.utf8 )
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
override func viewDidLoad() {
|
36
|
-
|
37
|
-
super.viewDidLoad()
|
38
24
|
|
39
25
|
|
40
26
|
|
41
|
-
|
27
|
+
} catch {
|
42
28
|
|
43
|
-
|
44
|
-
|
45
|
-
// 前に開いたファイルパスがあった場合
|
46
|
-
|
47
|
-
// **ここでエラー処理に入ってしまいます。**
|
48
|
-
|
49
|
-
if(!m_FilePath.isEmpty){
|
50
|
-
|
51
|
-
do{
|
52
|
-
|
53
|
-
// データを取得
|
54
|
-
|
55
|
-
let text = try String( contentsOfFile: m_FilePath, encoding: String.Encoding.utf8 )
|
56
|
-
|
57
|
-
} catch {
|
58
|
-
|
59
|
-
//エラー処理
|
60
|
-
|
61
|
-
|
29
|
+
print("error") // エラーになってしまいます。
|
62
|
-
|
63
|
-
}
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
}
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
// ----- ボタンが押された時
|
72
|
-
|
73
|
-
func btnTapped(){
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
// iCloudドキュメントを開く
|
78
|
-
|
79
|
-
if #available(iOS 11.0, *) {
|
80
|
-
|
81
|
-
let controller = UIDocumentPickerViewController.init(documentTypes: ["AAA"], in: .open)
|
82
|
-
|
83
|
-
controller.delegate = self
|
84
|
-
|
85
|
-
controller.allowsMultipleSelection = false
|
86
|
-
|
87
|
-
self.present(controller, animated: true, completion: nil)
|
88
|
-
|
89
|
-
}
|
90
|
-
|
91
|
-
else {
|
92
|
-
|
93
|
-
// Fallback on earlier versions
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
// ----- iCloudドキュメント内でファイルが選択された時
|
102
|
-
|
103
|
-
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){
|
104
|
-
|
105
|
-
if urls.first == nil || !urls.first!.startAccessingSecurityScopedResource(){ // アクセス許可
|
106
|
-
|
107
|
-
return
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
// パスを取得
|
114
|
-
|
115
|
-
let str = urls.first!.path
|
116
|
-
|
117
|
-
do{
|
118
|
-
|
119
|
-
// データを取得
|
120
|
-
|
121
|
-
let text = try String( contentsOfFile: str, encoding: String.Encoding.utf8 )
|
122
|
-
|
123
|
-
// 成功
|
124
|
-
|
125
|
-
m_FilePath = str
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
} catch {
|
130
|
-
|
131
|
-
//エラー処理
|
132
|
-
|
133
|
-
print("error")
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
urls.first!.stopAccessingSecurityScopedResource() // アクセス許可を取り消し
|
140
30
|
|
141
31
|
}
|
142
32
|
|
@@ -144,41 +34,9 @@
|
|
144
34
|
|
145
35
|
|
146
36
|
|
147
|
-
|
37
|
+
iCloudにあるデータを読み込むときは、何か特別な処理が必要でしょうか。
|
148
38
|
|
149
|
-
|
150
|
-
|
151
|
-
アクセス許可をしていないからではないかと思い、試しに以下のように変更しましたが、startAccessingSecurityScopedResource()でfalseが返ってきてしまい、処理が実行されませんでした。
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
```swift
|
156
|
-
|
157
|
-
|
39
|
+
良いやり方をご存じの方がいらっしゃいましたら、ご教示のほどお願い申し上げます。
|
158
|
-
|
159
|
-
if(!m_FilePath.isEmpty && url.startAccessingSecurityScopedResource()){
|
160
|
-
|
161
|
-
do{
|
162
|
-
|
163
|
-
// データを取得
|
164
|
-
|
165
|
-
let text = try String( contentsOfFile: m_FilePath, encoding: String.Encoding.utf8 )
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
} catch {
|
170
|
-
|
171
|
-
//エラー処理
|
172
|
-
|
173
|
-
print("error")
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
urls.first!.stopAccessingSecurityScopedResource()
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
```
|
182
40
|
|
183
41
|
|
184
42
|
|
1
タイトルと問題点を少し変更しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
iCloudにあるデータ読み込み
|
1
|
+
パスからiCloudにあるデータ読み込みができない
|
test
CHANGED
@@ -14,9 +14,7 @@
|
|
14
14
|
|
15
15
|
### 問題
|
16
16
|
|
17
|
-
iCloud
|
17
|
+
記憶したファイルパスを使い、iCloudにあるデータを読み込もうとしましたが、エラーになります。
|
18
|
-
|
19
|
-
(エラーになります。)
|
20
18
|
|
21
19
|
良いやり方をご存じの方がいらっしゃいましたら、ご教示のほどお願い申し上げます。
|
22
20
|
|