Home > Software engineering >  How to save images to Dropbox with UIActivityViewController | Swift/Xcode
How to save images to Dropbox with UIActivityViewController | Swift/Xcode

Time:03-16

I need to save photos directly from my app to 3rd Party file handlers (Dropbox, Egnyte, Google Drive). How do you upload images to Dropbox using the UIActivityViewController? "Upload to Egnyte" is an option, but there is nothing saying "Upload to Dropbox." I've tried each of the following:

Note: attemptOne shows the Egnyte and Dropbox symbols, but if I select those then it only uploads the first item in the array and not the rest. If I select "Upload to Egnyte" then all of the items are uploaded. Again, "Upload to Dropbox" isn't there, however. FYI, just so you're aware, attemptOne gives the photos custom names, while attemptTwo and attemptThree give the photos system-generated names that I'm not aware how to change.

func attemptOne() {
        var fileURLArray = [URL]()
        
        for data in reviewDataController.tableViewReviewData {
            guard let imageData = data.image.jpegData(compressionQuality: 1.0) else {
                print("ERROR: Unable to print convert image to jpegData in exportPhotosToFileLocation!")
                return
            }
            
            let fileManager = FileManager.default
            
            do {
                let fileURL = fileManager.temporaryDirectory.appendingPathComponent("\(data.imageTitle)").appendingPathExtension("jpeg")
                try imageData.write(to: fileURL)
                fileURLArray.append(fileURL)
                print("Successfully created file from jpegData in exportPhotosToFileLocation!")
            } catch {
                print("ERROR: Unable to create file from jpegData in exportPhotosToFileLocation!")
                return
            }
        }
        
        print("The number of items in fileURLArray is: \(fileURLArray.count)")
        print("The urls are:")
        for url in fileURLArray {
            print(url.absoluteString)
        }
        
        let vc = UIActivityViewController(activityItems: fileURLArray, applicationActivities: nil)
        present(vc, animated: true)
        
    }

func attemptTwo() {
        var uploadArray = [Data]()
        for data in reviewDataController.tableViewReviewData {
            if let imageData = data.image.jpegData(compressionQuality: 1.0) {
                uploadArray.append(imageData)
            }
        }
        let vc = UIActivityViewController(activityItems: uploadArray, applicationActivities: [])
        present(vc, animated: true)
    }

func attemptThree() {
        var uploadArray = [UIImage]()
        reviewDataController.tableViewReviewData.forEach { data in
            uploadArray.append(data.image)
        }
        let vc = UIActivityViewController(activityItems: uploadArray, applicationActivities: nil)
        present(vc, animated: true)
    }

I'm aware that there is a work-around through the Files application. You can load the Files application on your device, click the three dots in the upper-right-hand-corner, then enable the Dropbox switch. In your app, when the UIActivityViewController presents, you can click "Save to Files" then select Dropbox through Files. Unfortunately, this method does not always work. I've noticed that sometimes when I select Dropbox (through Files) and click Done that it uploads the photos. Other times, nothing happens. It's like the photos are lost in no-where land... nothing in Dropbox. Then there's other times where the photos will appear in Dropbox, but duplicated 2-3 times... as if those previous photos that were lost in no-where land, from previous attempts, all of a sudden decided they were going to save to Dropbox now too. As you can imagine, this is a HUGE user inconvenience. I simply need to find a way to upload images to Dropbox, using a UIActivityViewController, without cheating by going through "Files," but simply selecting "Save to Dropbox" on the main UIActivity home screen, right underneath "Save to Egnyte."

Please explain your answers and show how you implemented your solution with UIActivityViewController (in other words, show the UIActivityViewController code too) - I'm learning.

CodePudding user response:

Use SwiftyDropbox, its the official Dropbox SDK to send files directly to the user's dropbox account using your app.

For more see enter image description here

enter image description here

Then select dropbox and you will see this option to add your file to your dropbox... this is a built in feature of UIActivityController no need to write any code for this.

enter image description here

  • Related