Skip to content

Instantly share code, notes, and snippets.

@rahulsingh1101
Last active August 24, 2020 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsingh1101/557bf8927291d37127020685c0c6c1a4 to your computer and use it in GitHub Desktop.
Save rahulsingh1101/557bf8927291d37127020685c0c6c1a4 to your computer and use it in GitHub Desktop.
1. In capabilities need to turn on the iCloud and tick the iCloud Documnet in services. Choose the container and note
down its name. (because while setting up settings inside info.plist we. need to set the NSUbiquitousContainers key value
same as this name).
2. We need to define the iCloud Drive Container that we are going to use inside info.plist
<key>NSUbiquitousContainers</key>
<dict>
<key>this has to be same as selected containers name inside entitlement</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>Teszt</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
3. Need the url of the iCloud Container
var containerUrl: URL? {
// forUbiquityContainerIdentifier : Returns the URL for the iCloud container associated with the specified identifier
// and establishes access to that container.
return FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
}
// check for container existence
if let url = self.containerUrl, !FileManager.default.fileExists(atPath: url.path, isDirectory: nil) {
do {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
}
catch {
print(error.localizedDescription)
}
}
4. Copying to iCloud
func DownloadDocumnt() {
print("Selected URL: \(self.SelectedDownloadURL)")
let fileURL = URL(string: "\(self.SelectedDownloadURL)")!
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationFileUrl = documentsUrl.appendingPathComponent("Libra-asasas")
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-asasas")
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 {
print("Error : \(error.localizedDescription)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment