Best way to use iCloud Documents Storage

Best way to use iCloud Documents Storage

Content Index :

Best way to use iCloud Documents Storage
Tag : ios , By : Zinovate
Date : December 01 2020, 05:00 PM

fixed the issue. Will look into that further to the comment above and with further readings, I've find a way to solve my problem.
Here is how I decided to do it:
class CloudDataManager {

static let sharedInstance = CloudDataManager() // Singleton

struct DocumentsDirectory {
    static let localDocumentsURL: NSURL? = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: .UserDomainMask).last! as NSURL
   static let iCloudDocumentsURL: NSURL? = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("Documents")


// Return the Document directory (Cloud OR Local)
// To do in a background thread

func getDocumentDiretoryURL() -> NSURL {
    if userDefault.boolForKey("useCloud") && isCloudEnabled()  {
        return DocumentsDirectory.iCloudDocumentsURL!
    } else {
        return DocumentsDirectory.localDocumentsURL!

// Return true if iCloud is enabled

func isCloudEnabled() -> Bool {
    if DocumentsDirectory.iCloudDocumentsURL != nil { return true }
    else { return false }

// Delete All files at URL

func deleteFilesInDirectory(url: NSURL?) {
    let fileManager = NSFileManager.defaultManager()
    let enumerator = fileManager.enumeratorAtPath(url!.path!)
    while let file = enumerator?.nextObject() as? String {

        do {
            try fileManager.removeItemAtURL(url!.URLByAppendingPathComponent(file))
            print("Files deleted")
        } catch let error as NSError {
            print("Failed deleting files : \(error)")

// Move local files to iCloud
// iCloud will be cleared before any operation
// No data merging

func moveFileToCloud() {
    if isCloudEnabled() {
        deleteFilesInDirectory(DocumentsDirectory.iCloudDocumentsURL!) // Clear destination
        let fileManager = NSFileManager.defaultManager()
        let enumerator = fileManager.enumeratorAtPath(DocumentsDirectory.localDocumentsURL!.path!)
        while let file = enumerator?.nextObject() as? String {

            do {
                try fileManager.setUbiquitous(true,
                    itemAtURL: DocumentsDirectory.localDocumentsURL!.URLByAppendingPathComponent(file),
                    destinationURL: DocumentsDirectory.iCloudDocumentsURL!.URLByAppendingPathComponent(file))
                print("Moved to iCloud")
            } catch let error as NSError {
                print("Failed to move file to Cloud : \(error)")

// Move iCloud files to local directory
// Local dir will be cleared
// No data merging

func moveFileToLocal() {
    if isCloudEnabled() {
        let fileManager = NSFileManager.defaultManager()
        let enumerator = fileManager.enumeratorAtPath(DocumentsDirectory.iCloudDocumentsURL!.path!)
        while let file = enumerator?.nextObject() as? String {

            do {
                try fileManager.setUbiquitous(false,
                    itemAtURL: DocumentsDirectory.iCloudDocumentsURL!.URLByAppendingPathComponent(file),
                    destinationURL: DocumentsDirectory.localDocumentsURL!.URLByAppendingPathComponent(file))
                print("Moved to local dir")
            } catch let error as NSError {
                print("Failed to move file to local dir : \(error)")


No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

iCloud + Storage of media in iPhone Documents folder

Tag : iphone , By : rusl
Date : March 29 2020, 07:55 AM
hope this fix your issue iOS 5.0.1 introduced a flag to address this issue:
#include <sys/xattr.h>
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    const char* filePath = [[URL path] fileSystemRepresentation];

    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;

    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    return result == 0;

iCloud key value storage doesn't appear in Manage Storage in iCloud iPhone Setting

Tag : ios , By : Julian Ivanov
Date : March 29 2020, 07:55 AM
wish helps you i have enabled iCloud key value storage, and i have add some value,then i do this: , Only the UIDocument appears in that view.

Core data + iCloud:iCloud Storage only increase not decrease

Tag : ios , By : Thx1138.6
Date : March 29 2020, 07:55 AM
To fix this issue Core Data without iCloud works as you might expect. When you add data, the persistent store gets larger, and when you delete data it gets smaller.
Core Data with iCloud works by creating a baseline data store and then adding transaction logs that modify the baseline. When you add data, there's a new transaction that says to add the data. When you delete data, there's a new transaction that says to delete the data. But the transaction doesn't actually delete the data-- it just says that the data should be deleted, so that when transactions are replayed, the data is removed.

Unable to implement iCloud Drive/Files support whilst keeping existing iCloud documents. $(TeamIdentifierPrefix)

Tag : ios , By : wraith
Date : March 29 2020, 07:55 AM
wish help you to fix your issue As the title suggests, I want to implement iCloud Drive support on an existing app, in which the ubiquity container is prefixed by the old style, $(TeamIdentifierPrefix). For the life of me I cannot get this to work. , To get this working, the following entry was added to my Info.plist:
        <string>Example App Name</string>
struct UbiquityContainers {
    private static let ubiquityContainers : [String] = Bundle.main.object(forInfoDictionaryKey: "UbiquitousContainers") as! [String]
    static var iCloudDocuments : String {
        get {
            return ubiquityContainers[0]
    static var iCloudDrive : String {
        get {
            return ubiquityContainers[1]
var ubiquityContainer : String = UbiquityContainers.iCloudDrive
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if(self.documentsInCloud) {
        if(item == self.iCloudDriveTabBarItem) {
            self.ubiquityContainer = UbiquityContainers.iCloudDrive
        } else if(item == self.iCloudDocumentsTabBarItem) {
            self.ubiquityContainer = UbiquityContainers.iCloudDocuments

Apps must follow the iOS Data Storage Guidelines, nothing in /documents but still some kbs sent to iCloud

Tag : iphone , By : nickthecook
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Change status bar background color using UIStatusBarManager
  • Should we make tapGesture component an IBAction or IBOutlet to capture the tap event?
  • Iframe automatically goes full screen using Ionic 3.9.2 on iOS
  • How to calculate score out of 1000 given some seconds (In Double)
  • How to calculate time (minutes) between two dates in swift?
  • There was problem loading requested app.it looks like you may be using LAN URL
  • Swift circular corners doesn't work properly on different screen sizes
  • How change .navigationBarTitle font in SwiftUI?
  • What happens if `MPSCNNConvolutionDataSource` protocol's `load` method return false?
  • Why does the UIBarButtonItem not show up when I create it with code?
  • How to prevent following view controller inheriting navigation view controller bar?
  • What does it mean when .xcodeproj files are shown in red in Xcode?
  • Xcode SwiftUI how to scroll the Preview Canvas?
  • Expand tableview and increase scrollview content size by an appropriate difference on clicking a button inside a table c
  • xcodebuild failed with return code: 65 - using CLI/Appcenter but no error messages
  • "'isDeveloperModeEnabled' is deprecated: This no longer needs to be set during development. " What is isDevelo
  • Right click in UIKit for Mac
  • How we Support Dark Mode in Current iOS App with iOS 13?
  • Security vulnerablility issue in ios mobile app
  • How to set a background color for the viewController in swiftUI?
  • SwiftUI : Segue navigation
  • iOS 13 Custom UISearchBar _searchField crash
  • USDZ models have holes in SceneKit but not QuickLook
  • Why does iOS Password Autofill not prompt to save password on keychain?
  • Pushing information to firebase after event is created(Firebase-Realtime-Database)
  • Xcode 11 Beta and Carthage bootstrap
  • How to change the status bar background color and text color on iOS 13?
  • Using Core Data with SwiftUI
  • How should I do to wait all the data downloaded completely and show on TableView?
  • Can we specify the number of rows that can be shown in each section of a collection view
  • Why sometimes completion handler not called?
  • Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
  • In UIKit we can set label.numberOfLines = 0 when we do not know how many lines the text will take? What is the alternati
  • Swift ISO8601 format to Date returning fatal error
  • SwiftUI Login Page Layout
  • Xcode: Could not locate installed application. Install claimed to have succeeded, but application could not be found on
  • See the TestFlight crash log in the Xcode
  • Why does zero UIView's CALayer.speed stops UIAlertController's buttons from working?
  • Firestore collectionGroup query returns empty result
  • Modify text in SwiftUI
  • CNCopyCurrentNetworkInfo with iOS 13
  • WWDC 2019 - iOS13 April 2020 requirement
  • Nativescript IPhone X - How to "hide" home indicator behind two swipes to go to home screen
  • Should I use a singleton location manager in Swift?
  • Accessibility problem in reusable TableViewCell
  • Remove top line containing Done button from Keyboard Using Storyboard or in Swift
  • Swift Storyboard - Centring an Icon and multi-line text horizontally
  • viewDidAppear is delayed by 0.5 sec
  • Pass URL values to SecondView Controller WebView (WKWebView)
  • How to upload images faster on iOS, when using takePictureAsync?
  • Tableview multiple sections with one floating header
  • UITextView: Huge Memory Usage
  • How to put a logo in NavigationView in swiftui?
  • How can I load an UIImage into a SwiftUI Image asynchronously?
  • How to remove highlight on tap of List with SwiftUI?
  • how to use customized toolbar in multiple viewcontroller without repeating the code and copying the elements (swift)?
  • iPhone 7 simulator is not found on Xcode 11 beta
  • SwiftUI view is in the middle instead of in the top
  • How to get iOS 12 Autofill to ask to save password in React Native app?
  • Custom font none installed - Xcode 11 beta
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com