[ad_1]
iCloud drive mission setup tutorial
Let’s begin by creating a brand new mission for iOS. You possibly can choose the one view software template, don’t be concerned an excessive amount of about doc primarily based apps, as a result of on this tutorial we’re not going to the touch the UIDocument class in any respect. 🤷♂️
Step one is to allow iCloud capabilities, which is able to generate a brand new entitlements file for you. Additionally you may should allow the iCloud software service for the app id on the Apple developer portal. You also needs to assign the iCloud container that is going for use to retailer information. Just some clicks, however it’s important to do that manually. 💩
You want a legitimate Apple Developer Program membership with the intention to set superior app capabilities like iCloud assist. So it’s important to pay $99/yr. #greed 🤑
So I imagine that now you’ve gotten a correct iOS app identifier with iCloud capabilities and software providers enabled. One final step is forward, it’s important to add these few traces to your Data.plist file with the intention to outline the iCloud drive container (folder identify) that you’ll use. Notice that you may have a number of containers for one app.
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.tiborbodecs.teszt</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>Teszt</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
Lastly we’re prepared to maneuver ahead with some precise coding. 💻
Information inside iCloud drive containers
Working with iCloud information utilizing Swift is comparatively simple. Principally you simply should get the bottom url of your iCloud drive container, and you are able to do no matter you need. 🤔 Nevertheless I will present you some finest practices & tips.
First it’s important to verify in case your container folder already exists, if not you must create it by hand utilizing the FileManager class. I’ve additionally made a “shortcut” variable for the container base url, so I haven’t got to jot down all these lengthy phrases once more. 😅
var containerUrl: URL? {
return FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Paperwork")
}
if let url = self.containerUrl, !FileManager.default.fileExists(atPath: url.path, isDirectory: nil) {
do {
attempt FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
}
catch {
print(error.localizedDescription)
}
}
Working with paths contained in the iCloud drive container is easy, you’ll be able to append path parts to the bottom url and use that precise location url as you need.
let myDocumentUrl = self.containerUrl?
.appendingPathComponent(subDirectory)
.appendingPathComponent(fileName)
.appendingPathExtension(fileExtension)
Choosing current information can be fairly easy. You should utilize the built-in doc picker class from UIKit. There are solely two catches right here. 🤦♂️
First one is that you have to present the kind of the paperwork that you simply’d prefer to entry. Have you ever ever heard about UTI‘s? No? Possibly sure…? The factor is that it’s important to discover the correct uniform sort identifier for each file sort, as an alternative of offering an extension or mime-type or one thing generally used factor. Good one, huh? 🧠
let picker = UIDocumentPickerViewController(documentTypes: ["public.json"], in: .open)
picker.delegate = self
picker.modalPresentationStyle = .fullScreen
self.current(picker, animated: true, completion: nil)
The second catch is that it’s important to “unlock” the picked file earlier than you begin studying it. That may be completed by calling the startAccessingSecurityScopedResource methodology. Remember to name the stopAccessingSecurityScopedResource methodology, or issues are going to be out of stability. You don’t need that, belief me! #snap 🧤
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard
controller.documentPickerMode == .open,
let url = urls.first,
url.startAccessingSecurityScopedResource()
else {
return
}
defer {
url.stopAccessingSecurityScopedResource()
}
}
Every little thing else works as you’d count on. It can save you information immediately into the container by way of file APIs or by utilizing the UIDocumentPickerViewController occasion. Listed here are a number of the most typical api calls, that you should utilize to control information.
attempt string.write(to: url, atomically: true, encoding: .utf8)
attempt String(contentsOf: url)
attempt information.write(to: url, choices: [.atomic])
attempt Information(contentsOf: url)
FileManager.default.copyItem(at: native, to: url)
FileManager.default.removeItem(at: url)
You possibly can learn and write any form of string, information. Through the use of the FileManager you’ll be able to copy, transfer, delete objects or change file attributes. All of your paperwork saved inside iCloud drive will likely be magically obtainable on each gadget. Clearly it’s important to be logged in together with your iCloud account, and have sufficient free storage. 💰
Debugging
In case you alter one thing in your settings you would possibly need to increment your construct quantity as effectively with the intention to notify the working system in regards to the adjustments. 💡
On the mac all of the iCloud drive information / containers are situated beneath the consumer’s Library folder contained in the Cellular Paperwork listing. You possibly can merely use the Terminal or Finder to go there and record all of the information. Professional tip: search for hidden ones as effectively! 😉
cd ~/Library/Cellular Paperwork
ls -la
You can even monitor the exercise of the CloudDocs daemon, by utilizing this command:
brctl log --wait --shorten
The output will inform you what’s truly occurring through the sync.
I encourage you to verify the guide entry for the brctl command, as a result of there are a couple of extra flags that may make troubleshooting simpler. 🤐
This text was closely impressed by Marcin Krzyzanowski‘s actually outdated weblog publish. 🍺
[ad_2]
