Giter Club home page Giter Club logo

save-to-icloud-drive's Introduction

Save-To-iCloud-Drive

Save a document to iCloud drive

Steps to follow:

  1. Start new Project

  2. Enable iCloud Documents with Xcode, from tab Capabilities

  3. Create App Id and enable iCloud feature to your App Id

  4. Go to Info.plist of you application and add something similar to this.

    Note: You need to have NSUbiquitousContainerIsDocumentScopePublic=true in Info.plist before you run the app for the first time, Otherwise directory will not be shown in iCloud Drive, It will be a hidden directory.

    <key>NSUbiquitousContainers</key>
    <dict>
    	<key>iCloud.$(PRODUCT_BUNDLE_IDENTIFIER)</key>
    	<dict>
    		<key>NSUbiquitousContainerIsDocumentScopePublic</key>
    		<true/>
    		<key>NSUbiquitousContainerName</key>
    		<string>Folder_Name</string>
    		<key>NSUbiquitousContainerSupportedFolderLevels</key>
    		<string>Any</string>
    	</dict>
    </dict>
  5. In ViewController Create Directory

    func createDirectory(){
        if let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") {
            if (!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil)) {
                do {
                    try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
                }
                catch {
                    //Error handling
                    print("Error in creating doc")
                }
            }
        }
    }
  6. Then put all files inside to this Directory

    func copyDocumentsToiCloudDirectory() {
        guard let localDocumentsURL = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: .userDomainMask).last else { return }
        
        guard let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent("Subdirectory") else { return }
        
        var isDir:ObjCBool = false
        
        if FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &isDir) {
            do {
                try FileManager.default.removeItem(at: iCloudDocumentsURL)
            }
            catch {
                //Error handling
                print("Error in remove item")
            }
        }
        
        do {
            try FileManager.default.copyItem(at: localDocumentsURL, to: iCloudDocumentsURL)
        }
        catch {
            //Error handling
            print("Error in copy item")
        }
    }

    7: Run the application

save-to-icloud-drive's People

Contributors

anas-p avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.