快速将文档文件写入/保存/移动到iCloud驱动器 [英] Swift write/save/move a document file to iCloud drive
问题描述
我已经尝试了两天以上,将文件写入iCloud驱动器.我尝试直接在本地编写一个简单的文本文件,然后使用UIDocumentMenuViewController等将其移动.我的代码并没有逐步通过调试器,但没有出现任何错误,但是看起来很成功,但是当我检查文件是否存在或至少是iCloud目录,那里什么也没有.我在模拟器和iPhone上都进行了尝试,触发了iCloud同步以及其他所有我能想到的东西.
I've been trying for over two days to write a file to iCloud drive. I have tried writing a simple text file directly, locally then moving it, using UIDocumentMenuViewController, etc. I'm not getting any errors with my code and stepping through debugger, it looks successful, but when I check to see if the file exists or at least the iCloud directory, there is nothing there. I tried on both the simulator and my iPhone, triggering iCloud synching, and everything else I can think of.
我的主要目标是简单地将文本文件写入iCloud驱动器,以后将成为数字"文件
My main goal is to simply write a text file to the iCloud drive, which later will be "numbers" file
我已经设置了我的plist文件和权利:
I have set up my plist file and my 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>
我也按照捆绑版本进行了修改,如:保存iOS 8文档到iCloud Drive
I have also bumped up by bundle version as stated at: Save iOS 8 Documents to iCloud Drive
我没有运气就尝试了几十个教程.我的最新代码基于以下示例: https://medium .com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
I have tried dozens of tutorials with no luck. My latest code is based off of this sample: https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
这是我的代码:
@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);
}
感谢您抽出宝贵的时间.
Thank You for taking time for this.
干杯, 保罗
在上面的代码之后,我在iPhone上运行了一些代码:
I ran some 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)
}
并打印出我的文本文件的路径: 文件:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~paul~c~myApp/MyTextFile.txt
and it prints out the path to my text file: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~paul~c~myApp/MyTextFile.txt
我的文件在那里,只是在iCloud驱动器上看不到它.
My file is there, I just can't see it on iCloud drive.
推荐答案
我遇到了这个问题.我按照此处的建议,发现我的Info.plist
密钥不正确.一旦将其更改为iCloud.MY_BUNDLE_IDENTIFIER
(即从Info.plist
中较高位置的CFBundleIdentifier
键复制字符串),一切就开始起作用.
I had this problem. I followed the advice here and I found that my Info.plist
key was not correct. Once I changed it to iCloud.MY_BUNDLE_IDENTIFIER
(i.e. copy the string from the CFBundleIdentifier
key higher in Info.plist
) it all started working.
从密钥中删除.com可能会解决您的问题.
Removing the .com from your key may fix your issue.
这篇关于快速将文档文件写入/保存/移动到iCloud驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!