Close Menu
geekfence.comgeekfence.com
    What's Hot

    No magic bullet will solve the upper C-band

    March 18, 2026

    Is Learning Prompt Engineering Enough To Secure A Job In The AI And LLM Fields

    March 18, 2026

    SOTA Embedding Model for Agentic Workflows Now in Public Preview

    March 18, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»How to use iCloud drive documents?
    iOS Development

    How to use iCloud drive documents?

    AdminBy AdminMarch 18, 2026No Comments5 Mins Read0 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    How to use iCloud drive documents?
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Learn how to sync files and data through a shared iCloud drive folder using the latest version of Swift programming language.

    iCloud drive project setup tutorial

    Let’s start by creating a new project for iOS. You can select the single view application template, don’t worry too much about document based apps, because in this tutorial we’re not going to touch the UIDocument class at all. 🤷‍♂️

    How to use iCloud drive documents?

    The first step is to enable iCloud capabilities, which will generate a new entitlements file for you. Also you’ll have to enable the iCloud application service for the app id on the Apple developer portal. You should also assign the iCloud container that’s going to be used to store data. Just a few clicks, but you have to do this manually. 💩

    You need a valid Apple Developer Program membership in order to set advanced app capabilities like iCloud support. So you have to pay $99/year. #greed 🤑

    iCloud

    So I believe that now you have a proper iOS app identifier with iCloud capabilities and application services enabled. One last step is ahead, you have to add these few lines to your Info.plist file in order to define the iCloud drive container (folder name) that you’re going to use. Note that you can have multiple containers for one app.

    NSUbiquitousContainers
    
        iCloud.com.tiborbodecs.teszt
        
            NSUbiquitousContainerIsDocumentScopePublic
            
            NSUbiquitousContainerName
            Teszt
            NSUbiquitousContainerSupportedFolderLevels
            Any
        
    
    

    Finally we’re ready to move forward with some actual coding. 💻

    Files inside iCloud drive containers

    Working with iCloud files using Swift is relatively easy. Basically you just have to get the base URL of your iCloud drive container, and you can do whatever you want. 🤔 However I’ll show you some best practices & tricks.

    First you have to check if your container folder already exists, if not you should create it by hand using the FileManager class. I’ve also made a “shortcut” variable for the container base URL, so I don’t have to write all those long words again. 😅

    var containerUrl: URL? {
        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)
        }
    }
    

    Working with paths inside the iCloud drive container is simple, you can append path components to the base URL and use that exact location URL as you want.

    let myDocumentUrl = self.containerUrl?
        .appendingPathComponent(subDirectory)
        .appendingPathComponent(fileName)
        .appendingPathExtension(fileExtension)
    

    Picking existing files is also quite straightforward. You can use the built-in document picker class from UIKit. There are only two catches here. 🤦‍♂️

    First one is that you need to provide the type of the documents that you’d like to access. Have you ever heard about UTI’s? No? Maybe yes…? The thing is that you have to find the proper uniform type identifier for every file type, instead of providing an extension or mime-type or something commonly used thing. Smart one, huh? 🧠

    let picker = UIDocumentPickerViewController(
        documentTypes: ["public.json"], 
        in: .open
    )
    picker.delegate = self
    picker.modalPresentationStyle = .fullScreen
    self.present(picker, animated: true, completion: nil)
    

    The second catch is that you have to “unlock” the picked file before you start reading it. That can be done by calling the startAccessingSecurityScopedResource method. Don’t forget to call the stopAccessingSecurityScopedResource method, or things are going to be out of balance. You don’t want that, trust me! #snap 🧤

    func documentPicker(
        _ controller: UIDocumentPickerViewController, 
        didPickDocumentsAt urls: [URL]
    ) {
        guard
            controller.documentPickerMode == .open,
            let url = urls.first,
            url.startAccessingSecurityScopedResource()
        else {
            return
        }
        defer {
            url.stopAccessingSecurityScopedResource()
        }
        // do some work with the url
    }
    

    Everything else works as you’d expect. You can save files directly into the container through file APIs or by using the UIDocumentPickerViewController instance. Here are some of the most common api calls, that you can use to manipulate files.

    // string
    try string.write(to: url, atomically: true, encoding: .utf8)
    try String(contentsOf: url)
    
    // data
    try data.write(to: url, options: [.atomic])
    try Data(contentsOf: url)
    
    // file manager
    FileManager.default.copyItem(at: local, to: url)
    FileManager.default.removeItem(at: url)
    

    You can read and write any kind of string, data. By using the FileManager you can copy, move, delete items or change file attributes. All your documents stored inside iCloud drive will be magically available on every device. Obviously you have to be logged in with your iCloud account, and have enough free storage. 💰

    Debugging

    If you alter something in your settings you might want to increment your build number as well in order to notify the operating system about the changes. 💡

    On the mac all the iCloud drive files / containers are located under the user’s Library folder inside the Mobile Documents directory. You can simply use the Terminal or Finder to go there and list all the files. Pro tip: look for hidden ones as well! 😉

    cd ~/Library/Mobile\ Documents
    ls -la
    # ls -la|grep tiborbodecs
    

    You can also monitor the activity of the CloudDocs daemon, by using this command:

    # man brctl
    brctl log --wait --shorten
    

    The output will tell you what’s actually happening during the sync.

    Debug

    I encourage you to check the manual entry for the brctl command, because there are a few more flags that can make troubleshooting more easy. 🤐

    This article was heavily inspired by Marcin Krzyzanowski’s really old blog post. 🍺



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    ios – Video input to Shortcuts action

    March 17, 2026

    Future Updates | Cocoanetics

    March 13, 2026

    Swift simple factory design pattern

    March 12, 2026

    uikit – Why the title doesn’t follow the navigation inline state in iOS 26

    March 11, 2026

    I wish I could be an OpenClaw Maintainer

    March 7, 2026

    Swift factory method design pattern

    March 6, 2026
    Top Posts

    Hard-braking events as indicators of road segment crash risk

    January 14, 202620 Views

    Understanding U-Net Architecture in Deep Learning

    November 25, 202520 Views

    How to integrate a graph database into your RAG pipeline

    February 8, 202611 Views
    Don't Miss

    No magic bullet will solve the upper C-band

    March 18, 2026

    The FCC’s coming auction of the upper C-band for 5G and 6G and possible direct-to-device…

    Is Learning Prompt Engineering Enough To Secure A Job In The AI And LLM Fields

    March 18, 2026

    SOTA Embedding Model for Agentic Workflows Now in Public Preview

    March 18, 2026

    Cloud demand shifts toward AI as enterprise usage deepens

    March 18, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    No magic bullet will solve the upper C-band

    March 18, 2026

    Is Learning Prompt Engineering Enough To Secure A Job In The AI And LLM Fields

    March 18, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.