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

Swift将文档文件写入保存移动到iCloud驱动器,第1张

概述我已经尝试了两天多的时间来写一个文件到iCloud驱动器.我已经尝试直接编写一个简单的文本文件,然后在本地移动它,使用UIDocumentMenuViewController等.我没有得到任何错误的代码和单步调试,它看起来成功,但当我检查文件是否存在或至少iCloud目录,那里什么都没有.我尝试了模拟器和我的iPhone,触发iCloud同步,以及我能想到的其他一切. 我的主要目标是简单地将文本文 我已经尝试了两天多的时间来写一个文件到iCloud驱动器.我已经尝试直接编写一个简单的文本文件,然后在本地移动它,使用UIdocumentMenuVIEwController等.我没有得到任何错误的代码和单步调试,它看起来成功,但当我检查文件是否存在或至少iCloud目录,那里什么都没有.我尝试了模拟器和我的iPhone,触发iCloud同步,以及我能想到的其他一切.

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

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

<key>NSUbiquitousContainers</key><dict>    <key>iCloud.com.paul.c.$(PRODUCT_@R_419_6889@:rfc1034IDentifIEr)</key>    <dict>        <key>NSUbiquitousContainerIsdocumentScopePublic</key>        <true/>        <key>NSUbiquitousContainer@R_419_6889@</key>        <string>myCloudTest</string>        <key>NSUbiquitousContainerSupportedFolderLevels</key>        <string>Any</string>    </dict></dict>

我还提到了捆绑版本,如下所述:Save iOS 8 Documents to 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 documents / iCloud~com~paul~c~myApp / MyTextfile.txt

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

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

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

总结

以上是内存溢出为你收集整理的Swift将文档文件写入/保存/移动到iCloud驱动器全部内容,希望文章能够帮你解决Swift将文档文件写入/保存/移动到iCloud驱动器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1049543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论
请登录后评论...
登录
后才能评论 提交

评论列表(0条)
保存
{label}