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

我已经尝试了两天以上,将文件写入iCloud驱动器。我尝试直接在本地编写一个简单的文本文件,然后使用UIDocumentMenuViewController等将其移动。我的代码并没有逐步通过调试程序,但没有出现任何错误,但是看起来很成功,但是当我检查文件是否存在或至少iCloud目录中没有任何内容。我在模拟器和iPhone上都进行了尝试,触发了iCloud同步以及我能想到的所有其他功能。

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

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

<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>

我已经尝试了几十本没有运气的教程。我的最新代码基于以下示例: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驱动器上看不到它。

回答:

我有这个问题。我听了这里的建议,发现Info.plist钥匙不正确。一旦将其更改为iCloud.MY_BUNDLE_IDENTIFIER(即从中的CFBundleIdentifier较高键复制字符串Info.plist),所有内容都将开始工作。

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

以上是 快速将文档文件写入/保存/移动到iCloud驱动器 的全部内容, 来源链接: utcz.com/qa/434157.html

回到顶部