0

I have created a movie file and am able to save it locally onto the device. However I would like to take advantage of icloud documents and save it to icloud and also share it publicly. How does one do this using swift?

I found this link for saving general files, Save iOS 8 Documents to iCloud Drive. I'm going to guess that the methodology would be to save it locally first onto the device and then to save it to the cloud drive. But does anyone have a working sample of this? For example what is a possible implementation of the UIDocument functions below?

override func contents(forType typeName: String) throws -> Any {

}

override func load(fromContents contents: Any, ofType typeName: String?) throws {

}
Community
  • 1
  • 1
David Choi
  • 6,131
  • 10
  • 28
  • 28

2 Answers2

0

Unfortunately, it is next to impossible to "share" things with iCloud Drive. Use Dropbox. Here is the code to save your file, you need to setup the DropboxClientsManager.authorizedClient variable first of course, checkout their dev pages. Corepath is the path for your file.

let client = DropboxClientsManager.authorizedClient!
client.files.upload(path: corePath, mode: .overwrite, autorename: false, input: textData).response { response, error in

    if let metadata = response {
            // Get file (or folder) metadata
        }
        if let error = error {
            switch error {
            case .routeError(let boxed, let requestId):
                switch boxed.unboxed {
                case .path(let failedPath):
                        //rint("Failed update 2 path: \(failedPath)")

                        NotificationCenter.default.post(name: Notification.Name("dbFileCreationError"), object: nil, userInfo: nil)
                        break

                    default:
                        ////rint("Unknown \(error)")
                        break
                    }
            case .internalServerError(let code, let message, let requestId):
                ////rint("InternalServerError[\(requestId)]: \(code): \(message)")
                NotificationCenter.default.post(name: Notification.Name("dbInternalServerError"), object: nil, userInfo: nil)
                break
            case .badInputError(let message, let requestId):
                ////rint("BadInputError[\(requestId)]: \(message)")
                NotificationCenter.default.post(name: Notification.Name("dbBadInputError"), object: nil, userInfo: nil)
                break
            case .authError(let authError, let requestId):
                ////rint("AuthError[\(requestId)]: \(authError)")
                NotificationCenter.default.post(name: Notification.Name("dbAuthError"), object: nil, userInfo: nil)
                break
            case .rateLimitError(let rateLimitError, let requestId):
                ////rint("RateLimitError[\(requestId)]: \(rateLimitError)")
                NotificationCenter.default.post(name: Notification.Name("dbRateLimitError"), object: nil, userInfo: nil)
                break
            case .httpError(let code, let message, let requestId):
                ////rint("HTTPError[\(requestId)]: \(code): \(message)")
                NotificationCenter.default.post(name: Notification.Name("dbHTTPError"), object: nil, userInfo: nil)
                break
            default:
                break
                }
        }
    }
}
user3069232
  • 8,587
  • 7
  • 46
  • 87
  • Can you elaborate on why it's impossible to share things with icloud? – David Choi Apr 27 '17 at 18:28
  • David, cloud drive provides no API to share documents, period. You can share things thru cloudKit if your using assets, but your going to sharing to other appleID users thru a dedicated app. You can also share things via the activity app, which I can post the code too in a second answer. – user3069232 Apr 28 '17 at 04:39
  • That's very strange on apples part. Using dropbox as you suggested would I be able to share video files? Is there a charge or does it simply take up the user's currently allocated disk space? In other words whatever they are currently either getting for free or paying for. – David Choi Apr 30 '17 at 23:49
  • If you share a video on your dropbox; you effectively send them a link to said video. It doesn't use up and space on the person with whom you are sharing the video, unless they choose to save it [obviously]. – user3069232 May 01 '17 at 06:50
0

You can also share things, although not the way your thinking thru the activity app. The basic code to do so you can see here.

@IBAction func shareButtonClicked(sender: UIButton) {
let textToShare = "Swift is awesome!  Check out this website about it!"

if let myWebsite = NSURL(string: "http://www.codingexplorer.com/") {
    let objectsToShare = [textToShare, myWebsite]
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

    activityVC.popoverPresentationController?.sourceView = sender
    self.presentViewController(activityVC, animated: true, completion: nil)
}
}

This is explained in more detail in this web page.

http://www.codingexplorer.com/sharing-swift-app-uiactivityviewcontroller/

user3069232
  • 8,587
  • 7
  • 46
  • 87