iOS Swift 3以编程方式将文件复制到iCloud驱动器 [英] iOS Swift 3 Copy File to iCloud Drive Programatically

查看:270
本文介绍了iOS Swift 3以编程方式将文件复制到iCloud驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件下载选项.当用户从我的应用程序下载文档时,我需要将其存储到已经安装在用户移动设备中的用户iCloud Drive中.我已经在Web和Xcode中都配置了iCloud,但是问题是我无法正确将文件复制到iCloud Drive.文件已成功下载,并且也已移至iCloud,但文件不会出现在iCloud Drive App中.这是我尝试过的代码:

In my I'm having documents download option. When users downloading documents from my app I need to store it to users iCloud Drive which was install in users mobile already. I have configured iCloud in both web and in Xcode, but problem is I'm not able to copy files to iCloud Drive correctly. File was downloaded successfully, and also it moving to iCloud but files won't appearing in iCloud Drive App. Here is my tried code:

<key>NSUbiquitousContainers</key>
    <dict>
        <key>iCloud.MyAppBundleIdentifier</key>
        <dict>
            <key>NSUbiquitousContainerIsDocumentScopePublic</key>
            <true/>
            <key>NSUbiquitousContainerName</key>
            <string>iCloudDriveDemo</string>
            <key>NSUbiquitousContainerSupportedFolderLevels</key>
            <string>Any</string>
        </dict>
    </dict>

这是我的iCloud存储代码:

And Here is My iCloud Storage Code:

func DownloadDocumnt()
    {
        print("Selected URL: \(self.SelectedDownloadURL)")
        let fileURL = URL(string: "\(self.SelectedDownloadURL)")!

        let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
        let destinationFileUrl = documentsUrl.appendingPathComponent("Libra-\(self.SelectedDownloadFileName)")

        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)

        let request = URLRequest(url:fileURL)

        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil
            {
                if let statusCode = (response as? HTTPURLResponse)?.statusCode
                {
                    print("Successfully downloaded. Status code: \(statusCode)")
                }
                do
                {
                    if(FileManager.default.fileExists(atPath: destinationFileUrl.path))
                    {
                        try FileManager.default.removeItem(at: destinationFileUrl)
                        try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                    }
                    else
                    {
                        try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                    }

                    if let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
                    {

                        if(!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil))
                        {
                            try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
                        }
                    }

                    let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent("Libra-\(self.SelectedDownloadFileName)")

                    if let iCloudDocumentsURL = iCloudDocumentsURL
                    {
                        var isDir:ObjCBool = false
                        if(FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &isDir))
                        {
                            try FileManager.default.removeItem(at: iCloudDocumentsURL)
                            try FileManager.default.copyItem(at: tempLocalUrl, to: iCloudDocumentsURL)
                        }
                        else
                        {
                            try FileManager.default.copyItem(at: destinationFileUrl, to: iCloudDocumentsURL)
                        }
                    }

                }
                catch (let writeError)
                {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }
            }
            else
            {
                print("Error took place while downloading a file. Error description");
            }
        }
        task.resume()
    }

推荐答案

遇到此问题时,最有用的文章之一是以下链接:

One of the most useful articles when I was having this issue was at the following link:

技术问答QA1893为iCloud Drive更新iCloud容器的元数据

我也非常小心地删除并重新添加了info.plist条目.那似乎是行得通的.我最初是复制并粘贴在线资源中的条目.按键似乎不完全正确或包含一些隐藏字符,导致无法识别它们.

I also deleted and re-added my info.plist entries VERY CAREFULLY. That is what seemed to work. I originally copied and pasted the entries from an online resource. It seems that the keys were not exactly correct or contained some hidden characters which caused them not to be recognized.

在iCloud Drive中启用文档存储

这篇关于iOS Swift 3以编程方式将文件复制到iCloud驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆