Swift将文档文件写入/保存/移动到iCloud驱动器

前端之家收集整理的这篇文章主要介绍了Swift将文档文件写入/保存/移动到iCloud驱动器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经尝试了两天多的时间来写一个文件到iCloud驱动器.我已经尝试直接编写一个简单的文本文件,然后在本地移动它,使用UIDocumentMenuViewController等.我没有得到任何错误代码和单步调试,它看起来成功,但当我检查文件是否存在或至少iCloud目录,那里什么都没有.我尝试了模拟器和我的iPhone,触发iCloud同步,以及我能想到的其他一切.

我的主要目标是简单地将文本文件写入iCloud驱动器,后来将成为“数字”文件

我已经设置了我的plist文件和我的权利:

  1. <key>NSUbiquitousContainers</key>
  2. <dict>
  3. <key>iCloud.com.paul.c.$(PRODUCT_NAME:rfc1034identifier)</key>
  4. <dict>
  5. <key>NSUbiquitousContainerIsDocumentScopePublic</key>
  6. <true/>
  7. <key>NSUbiquitousContainerName</key>
  8. <string>myCloudTest</string>
  9. <key>NSUbiquitousContainerSupportedFolderLevels</key>
  10. <string>Any</string>
  11. </dict>
  12. </dict>

我还提到了捆绑版本,如下所述:Save iOS 8 Documents to iCloud Drive

我已经尝试了几十个没有运气的教程.我的最新代码基于此示例:https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1

这是我的代码

  1. @IBAction func ExportFile(sender: AnyObject) {
  2.  
  3. var error:NSError?
  4.  
  5. let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("myCloudTest")
  6.  
  7. //is iCloud working?
  8. if iCloudDocumentsURL != nil {
  9.  
  10. //Create the Directory if it doesn't exist
  11. if (!NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!,isDirectory: nil)) {
  12. //This gets skipped after initial run saying directory exists,but still don't see it on iCloud
  13. NSFileManager.defaultManager().createDirectoryAtURL(iCloudDocumentsURL!,withIntermediateDirectories: true,attributes: nil,error: nil)
  14. }
  15. } else {
  16. println("iCloud is NOT working!")
  17. // return
  18. }
  19.  
  20. if ((error) != nil) {
  21. println("Error creating iCloud DIR")
  22. }
  23.  
  24.  
  25. //Set up directorys
  26. let localDocumentsURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory,inDomains: .UserDomainMask).last as! NSURL
  27.  
  28. //Add txt file to my local folder
  29. let myTextString = NSString(string: "HELLO WORLD")
  30. let myLocalFile = localDocumentsURL.URLByAppendingPathComponent("myTextFile.txt")
  31. let written = myTextString.writeToURL(myLocalFile,atomically: true,encoding: NSUTF8StringEncoding,error: &error)
  32.  
  33. if ((error) != nil){
  34. println("Error saving to local DIR")
  35. }
  36.  
  37.  
  38. //If file exists on iCloud remove it
  39. var isDir:ObjCBool = false
  40. if (NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!,isDirectory: &isDir)) {
  41. NSFileManager.defaultManager().removeItemAtURL(iCloudDocumentsURL!,error: &error)
  42. }
  43.  
  44. //copy from my local to iCloud
  45. if (error == nil && !NSFileManager.defaultManager().copyItemAtURL(localDocumentsURL,toURL: iCloudDocumentsURL!,error: &error)) {
  46. println(error?.localizedDescription);
  47. }

感谢您抽出宝贵时间.

干杯,
保罗

我在上面的代码之后在我的iphone上运行了一些代码

  1. var error:NSError?
  2. let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil) //?.URLByAppendingPathComponent("myCloudTest")
  3.  
  4. var fileManager: NSFileManager = NSFileManager()
  5.  
  6.  
  7. var fileList: NSArray = fileManager.contentsOfDirectoryAtURL(iCloudDocumentsURL!,includingPropertiesForKeys: nil,options: nil,error: &error)!
  8. var filesStr: NSMutableString = NSMutableString(string: "Files in iCloud folder \n")
  9. for s in fileList {
  10.  
  11. println(s)
  12. }

它打印出我的文本文件的路径:
file:/// private / var / mobile / Library / Mobile Documents / iCloud~com~paul~c~myApp / MyTextFile.txt

我的文件在那里,我在iCloud驱动器上看不到它.

我有这个问题.我 followed the advice here和我发现我的Info.plist键不正确.一旦我将其更改为iCloud.MY_BUNDLE_IDENTIFIER(即从Info.plist中更高的CFBundleIdentifier键复制字符串),它就开始工作了.

从密钥中删除.com可能会解决您的问题.

猜你在找的Swift相关文章