-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathTodayListManager.swift
58 lines (43 loc) · 2.23 KB
/
TodayListManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
The `TodayListManager` class implements convenience methods to create and retrieve the Today list document from the user's ubiquity container.
*/
import Foundation
public class TodayListManager {
/**
Fetches the ubiquity container URL for the Today list document. If one isn't found, the block is invoked
with a `nil` value.
*/
public class func fetchTodayDocumentURLWithCompletionHandler(completionHandler: (url: NSURL?) -> Void) {
let defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(defaultQueue) {
let url = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)
let successURL = self.createTodayDocumentURLWithContainerURL(url)
completionHandler(url: successURL)
}
}
public class func createTodayDocumentURLWithContainerURL(containerURL: NSURL?) -> NSURL? {
if containerURL == nil {
return nil
}
let todayDocumentFolderURL = containerURL!.URLByAppendingPathComponent("Documents")!
let todayDocumentURL = todayDocumentFolderURL.URLByAppendingPathComponent(AppConfiguration.localizedTodayDocumentName)!.URLByAppendingPathExtension(AppConfiguration.listerFileExtension)!
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(todayDocumentURL.path!) {
return todayDocumentURL
}
do {
try fileManager.createDirectoryAtURL(todayDocumentFolderURL, withIntermediateDirectories: true, attributes: nil)
let sampleTodayDocumentURL = NSBundle(forClass: self).URLForResource("Today", withExtension: AppConfiguration.listerFileExtension)
try fileManager.copyItemAtURL(sampleTodayDocumentURL!, toURL: todayDocumentURL)
// Make the file's extension hidden.
try fileManager.setAttributes([NSFileExtensionHidden: true], ofItemAtPath: todayDocumentURL.path!)
return todayDocumentURL
}
catch {
return nil
}
}
}