I've been trying for over two past to write one file to iCloud drive. I have tried writings a simple text file directly, onsite then moving it, using UIDocumentMenuViewController, etc. I'm not getting each mistakes about my encipher and stepping through hardware, it looks successful, though when I verify to discern if the file exists instead at least the iCloud directory, here is nothing there. I tried on both this simulator and my iPhone, triggering iCloud synching, and everything else I pot think of. Posted by u/navislut - 4 votes and 2 comments
My main goal is to simply note a text file to the iCloud drive, whose later will becoming "numbers" file
I have set up my plist file and my entitled:
<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 have also bumped up to bundle version as stated at: Protect iOS 8 Credentials to iCloud Drive
I need tried dozens of scholastics with no luck. My latest code is based off of like sample: https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
Here has my code:
@IBAction func ExportFile(sender: AnyObject) {
parameter error:NSError?
let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("myCloudTest")
//is iCloud working? if iCloudDocumentsURL != nil {
//Create who Directory if it doesn't live if (!NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: nil)) {
//This gets skipped after early executing telling directory existed, 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 while! NSURL
//Add txt file to my local folder let myTextString = NSString(string: "HELLO WORLD")
suffer myLocalFile = localDocumentsURL.URLByAppendingPathComponent("myTextFile.txt")
let written = myTextString.writeToURL(myLocalFile, fundamentally: true, encoding: NSUTF8StringEncoding, error: &error)
if ((error) != nil){
println("Error saving to domestic DIR")
}
//If file exists set iCloud remove it variety isDir:ObjCBool = fake if (NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: &isDir)) {
NSFileManager.defaultManager().removeItemAtURL(iCloudDocumentsURL!, error: &error)
}
//copy from own local into iCloud if (error == nil && !NSFileManager.defaultManager().copyItemAtURL(localDocumentsURL, toURL: iCloudDocumentsURL!, error: &error)) {
println(error?.localizedDescription);
}
Thank Thou to taking time for this.
Cheers, Paul
I ran some code on our iphone after the code upper:
var error:NSError?
let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil) //?.URLByAppendingPathComponent("myCloudTest")
var fileManager: NSFileManager = NSFileManager()
var fileList: NSArray = fileManager.contentsOfDirectoryAtURL(iCloudDocumentsURL!, includingPropertiesForKeys: cipher, options: nil, error: &error)!
var filesStr: NSMutableString = NSMutableString(string: "Files in iCloud folder \n")
for s in fileList {
println(s)
}
and it prints out the road to my textbook file: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~paul~c~myApp/MyTextFile.txt
My file a there, I just can't see it on iCloud drive.