我已经尝试了两天以上将文件写入iCloud驱动器。我尝试过直接编写简单文本文件,然后将其移动到本地,使用UIDocumentMenuViewController等方法。我的代码没有出现任何错误,并且在调试器中步进时看起来是成功的,但是当我检查文件是否存在或至少iCloud目录是否存在时,没有找到任何内容。我在模拟器和iPhone上都尝试过,触发了iCloud同步以及我能想到的其他所有的方法。
我的主要目标是将一个文本文件简单地写入iCloud驱动器,稍后它将成为"numbers"文件。
我已经设置了plist文件和我的entitlements:
我已按照以下链接所述,提升了我的捆绑版本号: 将iOS 8文档保存到iCloud Drive 我尝试了数十个教程,但没有成功。 我最新的代码是基于这个示例构建的:https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1 以下是我的代码:
我的主要目标是将一个文本文件简单地写入iCloud驱动器,稍后它将成为"numbers"文件。
我已经设置了plist文件和我的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 我尝试了数十个教程,但没有成功。 我最新的代码是基于这个示例构建的: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);
}
感谢您抽出时间来阅读。
祝愉快, 保罗
我在上述代码后在我的iPhone上运行了一些代码:
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)
}
它打印出了我的文本文件的路径:file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~paul~c~myApp/MyTextFile.txt
我的文件在那里,我只是在iCloud驱动器上看不到它。