WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: How to preinstall data on an app using CloudKit and iCloud ?

Forums > SwiftUI

i am unable to decide on what would be the best possible way to go ahead with the task of

Having 50 large video files with 50 large images corresponding to those files and some related text prepopulated in iCloud so that when the user downloads the app , they have some videos already installed to watch and not have to start from scratch…

I am aware of how to upload text to iCloud using some code as below , but I do. not know how will I proceed with large media files, where will I store them , how will I point to them ?

If a user wants to play those videos, will those videos need to be downloaded to the user device ? Will this not increase the size of app significantly ? How can I avoid this ?

All in all I am totally lost as to how to deal with large size files, how to prepopulate my app with some of these files and how to add more files in future ,

Can any one please guide and point me to a plan of action I can take in this regard ...

The code I have to be able to prepare and upload some data to iCloud , but I do not know how to use them when using large media files, where will I store those files etc

func prepareCloudRecords(owner: String) -> [CKRecord] {
        let parentName = objectID.uriRepresentation().absoluteString
        let parentID = CKRecord.ID(recordName: parentName)
        let parent = CKRecord(recordType: "Project", recordID: parentID)
        parent["title"] = projectTitle
        parent["detail"] = projectDetail
        parent["owner"] = owner
        parent["closed"] = closed

        var records = projectItemsDefaultSorted.map { item -> CKRecord in
            let childName = item.objectID.uriRepresentation().absoluteString
            let childID = CKRecord.ID(recordName: childName)
            let child = CKRecord(recordType: "Item", recordID: childID)
            child["title"] = item.itemTitle
            child["detail"] = item.itemDetail
            child["completed"] = item.completed
            child["project"] = CKRecord.Reference(recordID: parentID, action: .deleteSelf)
            return child
        return records

Then use it to upload to server on button click like so making a function, thanks

func uploadToCloud() {
        if let username = username {
            let records = project.prepareCloudRecords(owner: username)
            let operation = CKModifyRecordsOperation(recordsToSave: records, recordIDsToDelete: nil)
            operation.savePolicy = .allKeys
            operation.modifyRecordsCompletionBlock = { _, _, error in

                   cloudStatus = .checking

        } else {
            showingSignIn = true


Amit asks:

I am totally lost as to how to deal with large size files, how to prepopulate my app with some of these files and how to add more files in future. Can any one please guide and point me to a plan of action I can take in this regard?

This sounds like a business problem, not an issue with iOS, or your application.

If you are going to sell your application with a goal to make money (rupees?), you'll become a businessperson. Your content (defined by you as large sized files) is the currency that drives your customer to pay you for your application.

Don't reinvent the wheel! How do other successful businesses do it? Take a look at our leader @twoStraws! What is his business model?

@twoStraws has a great application, Unwrap, on the Apple Store. (You DO HAVE IT, correct?) Under the tab "Learning" you can tap a lesson and read about a topic. You can also watch a video on the same topic! I dont know if you'd call these large sized files, but think about where you've seen these before? These same videos can be found on YouTube.

So what's his business model? When you buy Unwrap from the AppleStore, does it come pre-loaded with EVERY single video? Or is there an option when you first launch the app to download the first 10 videos? Or maybe there's an option to download ALL the videos in the background? Or maybe he doesn't store any videos and requires you to have a net connection.

What is the size of the app when you first download it? Make sure you are in Airplane mode and not connected. What is the size of the app? Try to view some videos. Are they loaded? How much space does it use on your device.

Then test, and test again. Go online, watch a dozen videos. Are they stored locally for future (off line) reference? Does the storage space on your device change?

Frankly, I don't remember. But you can delete your application, and download it again. Study his model and make notes.


@Obelix - thanks, yes i guess trying to reinvent the wheel is not the way to go, i was able to find the way needed to do this which was to use CKAsset, but i will not do it ...

I am trying to expand on the UltimatePortfolio project by using audio and video files in place of plain text as data ...


Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.