Swift write/save/move a document file to iCloud drive -
i've been trying on 2 days write file icloud drive. have tried writing simple text file directly, locally moving it, using uidocumentmenuviewcontroller, etc. i'm not getting errors code , stepping through debugger, looks successful, when check see if file exists or @ least icloud directory, there nothing there. tried on both simulator , iphone, triggering icloud synching, , else can think of.
my main goal write text file icloud drive, later "numbers" file
i have set plist file , entitlements:
<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 bumped bundle version stated at: save ios 8 documents icloud drive
i have tried dozens of tutorials no luck. latest code based off of sample: https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
here code:
@ibaction func exportfile(sender: anyobject) { var error:nserror? let iclouddocumentsurl = nsfilemanager.defaultmanager().urlforubiquitycontaineridentifier(nil)?.urlbyappendingpathcomponent("mycloudtest") //is icloud working? if iclouddocumentsurl != nil { //create directory if doesn't exist if (!nsfilemanager.defaultmanager().fileexistsatpath(iclouddocumentsurl!.path!, isdirectory: nil)) { //this gets skipped after initial run saying directory exists, still don't see on icloud nsfilemanager.defaultmanager().createdirectoryaturl(iclouddocumentsurl!, withintermediatedirectories: true, attributes: nil, error: nil) } } else { println("icloud not working!") // return } if ((error) != nil) { println("error creating icloud dir") } //set directorys let localdocumentsurl = nsfilemanager.defaultmanager().urlsfordirectory(nssearchpathdirectory.documentdirectory, indomains: .userdomainmask).last as! nsurl //add txt file 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 local dir") } //if file exists on icloud remove var isdir:objcbool = false if (nsfilemanager.defaultmanager().fileexistsatpath(iclouddocumentsurl!.path!, isdirectory: &isdir)) { nsfilemanager.defaultmanager().removeitematurl(iclouddocumentsurl!, error: &error) } //copy local icloud if (error == nil && !nsfilemanager.defaultmanager().copyitematurl(localdocumentsurl, tourl: iclouddocumentsurl!, error: &error)) { println(error?.localizeddescription); }
thank taking time this.
cheers, paul
i ran code on iphone after 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") s in filelist { println(s) }
and prints out path text file: file:///private/var/mobile/library/mobile%20documents/icloud~com~paul~c~myapp/mytextfile.txt
my file there, can't see on icloud drive.
i had problem. followed advice here , found info.plist
key not correct. once changed icloud.my_bundle_identifier
(i.e. copy string cfbundleidentifier
key higher in info.plist
) started working.
removing .com key may fix issue.
Comments
Post a Comment