I am currently using local storage in my iOS app. User data is stored in the document directory, and now I plan to use iCloud document storage instead.
This is how I intend to do this:
Check for iCloud on your device
If yes, use the URLForUbiquityContainerIdentifier to get the URL of the iCloud container
Save new files and documents to this new URL
For this, I use this code, which will return the URL of the document folder (iCloud or local)
class CloudDataManager {
class func getDocumentDiretoryURL() -> NSURL {
let localDocumentsURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: .UserDomainMask).last! as NSURL
let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("Documents")
if userDefault.boolForKey("useCloud") && iCloudDocumentsURL != nil {
return iCloudDocumentsURL!
} else {
return localDocumentsURL
}
}
}
Is this the best practice? I am worried that problems will arise if one day iCloud is unavailable, so the local directory will be used instead of the cloud container and will be empty. Thanks.