質問編集履歴
1
続きです。
test
CHANGED
File without changes
|
test
CHANGED
@@ -257,3 +257,239 @@
|
|
257
257
|
```
|
258
258
|
|
259
259
|
以下続きます。
|
260
|
+
|
261
|
+
```
|
262
|
+
|
263
|
+
print("Documentデータを保存しました。")
|
264
|
+
|
265
|
+
let error:NSError? = nil
|
266
|
+
|
267
|
+
DispatchQueue.global(qos: .default).async {
|
268
|
+
|
269
|
+
do{
|
270
|
+
|
271
|
+
try FileManager.default.setUbiquitous(true, itemAt: localFileUrl, destinationURL: fileUrl!)
|
272
|
+
|
273
|
+
} catch let error {
|
274
|
+
|
275
|
+
print("エラー内容:\(error)")
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
if success {
|
280
|
+
|
281
|
+
print("iCloudコンテナへの移動に成功しました")
|
282
|
+
|
283
|
+
} else {
|
284
|
+
|
285
|
+
if let err = error {
|
286
|
+
|
287
|
+
// print("iCloudコンテナへの移動に失敗しました: \(err.description)")
|
288
|
+
|
289
|
+
print("iCloudコンテナへの移動に失敗しました: \(err.localizedDescription)")
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
} else {
|
300
|
+
|
301
|
+
print("Documentデータを保存できませんでした。")
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
})
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
// MARK: - Notification
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
func metadataQueryDidChange(_ notification: Notification) {
|
318
|
+
|
319
|
+
print("metadataQueryDidChange")
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
query.stop()
|
324
|
+
|
325
|
+
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
let metadataItems = query.results as! [NSMetadataItem]
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
let urls = metadataItems.map({ (item: NSMetadataItem) -> URL in
|
334
|
+
|
335
|
+
let url = item.value(forAttribute: NSMetadataItemURLKey) as! URL
|
336
|
+
|
337
|
+
print("iCloud Storage: \(url)")
|
338
|
+
|
339
|
+
return url
|
340
|
+
|
341
|
+
}).filter({ (url: URL) -> Bool in
|
342
|
+
|
343
|
+
if let fileName = url.path.components(separatedBy: "/").last {
|
344
|
+
|
345
|
+
return fileName == USDocInfo.NAME + "." + USDocInfo.EXTENSION
|
346
|
+
|
347
|
+
} else {
|
348
|
+
|
349
|
+
return false
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
})
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
if 0 < urls.count {
|
358
|
+
|
359
|
+
self.document = USDocument(fileURL: urls.first!)
|
360
|
+
|
361
|
+
self.document.open(completionHandler: { (success:Bool) in
|
362
|
+
|
363
|
+
if success {
|
364
|
+
|
365
|
+
print("Documentを開きました。place: \(String(describing: self.document.place))")
|
366
|
+
|
367
|
+
} else {
|
368
|
+
|
369
|
+
print("Documentを開けませんでした。")
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
})
|
374
|
+
|
375
|
+
} else {
|
376
|
+
|
377
|
+
print("該当ファイルがありません")
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
func metadataQueryDidUpdate(_ notification: Notification) {
|
388
|
+
|
389
|
+
print("metadataQueryDidUpdate")
|
390
|
+
|
391
|
+
query.disableUpdates()
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
var insertedURLs = [URL]()
|
398
|
+
|
399
|
+
var removedURLs = [URL]()
|
400
|
+
|
401
|
+
var updatedURLs = [URL]()
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
let metadataItemToURLTransform: (NSMetadataItem) -> URL = { metadataItem in
|
406
|
+
|
407
|
+
return metadataItem.value(forAttribute: NSMetadataItemURLKey) as! URL
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
let insertedMetadataItems = notification.userInfo?[NSMetadataQueryUpdateAddedItemsKey] as! [NSMetadataItem]?
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
if let insertedMetadataItems = insertedMetadataItems {
|
418
|
+
|
419
|
+
insertedURLs += insertedMetadataItems.map(metadataItemToURLTransform)
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
let removedMetadataItems = notification.userInfo?[NSMetadataQueryUpdateRemovedItemsKey] as! [NSMetadataItem]?
|
426
|
+
|
427
|
+
if let removedMetadataItems = removedMetadataItems {
|
428
|
+
|
429
|
+
removedURLs += removedMetadataItems.map(metadataItemToURLTransform)
|
430
|
+
|
431
|
+
}
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
let updatedMetadataItems = notification.userInfo?[NSMetadataQueryUpdateChangedItemsKey] as! [NSMetadataItem]?
|
436
|
+
|
437
|
+
if let updatedMetadataItems = updatedMetadataItems {
|
438
|
+
|
439
|
+
let completelyDownloadedUpdatedMetadataItems = updatedMetadataItems.filter { updatedMetadataItem in
|
440
|
+
|
441
|
+
let downloadStatus = updatedMetadataItem.value(forAttribute: NSMetadataUbiquitousItemDownloadingStatusKey) as! String
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
return downloadStatus == NSMetadataUbiquitousItemDownloadingStatusCurrent
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
updatedURLs += completelyDownloadedUpdatedMetadataItems.map(metadataItemToURLTransform)
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
query.enableUpdates()
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
func iCloudDocumentDidChange(_ notification: Notification) {
|
464
|
+
|
465
|
+
if self.document.documentState.contains(.inConflict){
|
466
|
+
|
467
|
+
do{
|
468
|
+
|
469
|
+
try NSFileVersion.removeOtherVersionsOfItem(at: self.document.fileURL)
|
470
|
+
|
471
|
+
} catch let error {
|
472
|
+
|
473
|
+
print("エラー内容:\(error)")
|
474
|
+
|
475
|
+
}
|
476
|
+
|
477
|
+
if let conflicts = NSFileVersion.unresolvedConflictVersionsOfItem(at: self.document.fileURL){
|
478
|
+
|
479
|
+
for fileVersion in conflicts {
|
480
|
+
|
481
|
+
fileVersion.isResolved = true
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
```
|