Skip to content

Instantly share code, notes, and snippets.

@fel-cesar
Created July 11, 2018 20:32
Show Gist options
  • Save fel-cesar/2d5ba106c072ba978c48c7f3b8e3d99e to your computer and use it in GitHub Desktop.
Save fel-cesar/2d5ba106c072ba978c48c7f3b8e3d99e to your computer and use it in GitHub Desktop.
// SwiftViewController.swift
// PrototypeApplication
//
// Created by Felipe César Silveira de Assis on 12/12/17.
// Copyright © 2017 CloudRail. All rights reserved.
//
import UIKit
import CloudrailSI
class SwiftViewController: UIViewController, CRUploadProgressDelegate{
var currentProgress = Float(0);
var filesize = 6903974.0;
var uploaded = 0.0;
@IBAction func buttonAction(_ sender: Any) {
DispatchQueue.global(qos: .background).async {
print("This will run in the background queue")
self.basicDropbox()
}
}
fileprivate func basicDropbox() {
CRCloudRail.setAppKey("[CLOUDRAIL_APP]")
let dropbox = Dropbox.init(clientId: "[DROPBOX_KEY]", clientSecret: "[DROPBOX_SECRET]")
dropbox.setDelegate(self);
dropbox.setTarget(self)
let imageStream = InputStream.init(fileAtPath: "/[PATH_TO_FILE]/IMG_0018.JPG")!
let imageStreamCustom = CustomStream.init(stream: imageStream)! // IMPORTANT: Use Custom Stream!!!
imageStreamCustom.uploadProgressDelegate = self
self.currentProgress = Float(0)
do {
try print(dropbox.userLogin() ?? "nil value FOUND")
try dropbox.uploadFileToPath("/justUploaded.jpg", stream: imageStreamCustom, size: 6903974, overwrite: true)
} catch {
print("Exception: \(error)")
}
}
//MARK - UploadProgressDelegate
func didUploadBytes(_ bytes: Int) {
self.currentProgress = (Float(bytes)/Float(filesize))*100
print("Uploaded: \(self.currentProgress)%")
}
func didCancelUpload() -> Bool {
if self.currentProgress > 50.0 { // will cancel whenever the upload gets above 50%
return true
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment