I have been trying for two days to write a file on iCloud drive. I tried to write a simple text file directly, locally, then moving it using UIDocumentMenuViewController etc. I do not get any errors with my code and do not step over the debugger, it looks successful, but when I check if the file exists, or at least in the iCloud directory, there is nothing there. I tried both the simulator and my iPhone, starting iCloud sync and everything else I can think of.
My main goal is to simply write a text file to iCloud, which will later be a "number" file
I installed my plist file and my rights are:
<key>NSUbiquitousContainers</key> <dict> <key>iCloud.com.paul.c.$(PRODUCT_NAME:rfc1034identifier)</key> <dict> <key>NSUbiquitousContainerIsDocumentScopePublic</key> <true/> <key>NSUbiquitousContainerName</key> <string>myCloudTest</string> <key>NSUbiquitousContainerSupportedFolderLevels</key> <string>Any</string> </dict> </dict>
I also came across a package version as stated in: Save iOS 8 documents to iCloud Drive
I tried dozens of tutorials with no luck. My last code is based on this example: https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
Here is my code:
@IBAction func ExportFile(sender: AnyObject) { var error:NSError? let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("myCloudTest") //is iCloud working? if iCloudDocumentsURL != nil { //Create the Directory if it doesn't exist if (!NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: nil)) { //This gets skipped after initial run saying directory exists, but still don't see it on iCloud NSFileManager.defaultManager().createDirectoryAtURL(iCloudDocumentsURL!, withIntermediateDirectories: true, attributes: nil, error: nil) } } else { println("iCloud is NOT working!") // return } if ((error) != nil) { println("Error creating iCloud DIR") } //Set up directorys let localDocumentsURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: .UserDomainMask).last as! NSURL //Add txt file to my local folder let myTextString = NSString(string: "HELLO WORLD") let myLocalFile = localDocumentsURL.URLByAppendingPathComponent("myTextFile.txt") let written = myTextString.writeToURL(myLocalFile, atomically: true, encoding: NSUTF8StringEncoding, error: &error) if ((error) != nil){ println("Error saving to local DIR") } //If file exists on iCloud remove it var isDir:ObjCBool = false if (NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: &isDir)) { NSFileManager.defaultManager().removeItemAtURL(iCloudDocumentsURL!, error: &error) } //copy from my local to iCloud if (error == nil && !NSFileManager.defaultManager().copyItemAtURL(localDocumentsURL, toURL: iCloudDocumentsURL!, error: &error)) { println(error?.localizedDescription); }
Thanks for taking the time to do this.
Cheers, Paul
I ran the code on my iphone after the code above:
var error:NSError? let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil) //?.URLByAppendingPathComponent("myCloudTest") var fileManager: NSFileManager = NSFileManager() var fileList: NSArray = fileManager.contentsOfDirectoryAtURL(iCloudDocumentsURL!, includingPropertiesForKeys: nil, options: nil, error: &error)! var filesStr: NSMutableString = NSMutableString(string: "Files in iCloud folder \n") for s in fileList { println(s) }
and it prints the path to my text file: File: ///private/var/mobile/Library/Mobile%20Documents/iCloud~com~paul~c~myApp/MyTextFile.txt
My file is there, I just do not see it on the iCloud drive.