Search code examples
iosobjective-cevernote

Evernote: How can I get the upload progress for a note?


My app records audio, then creates a ENNote with the resulting audio file as a resource (and the current date and time as title), and uploads this note to Evernote. The user already gets notified when the upload is complete. Now I'd also like to show the upload progress. So I'm using this function:

ENSession.shared.upload(note, policy: .create, to: nil, orReplace: nil, progress: progressHandler, completion: completionHandler)

Everything works, except that the progressHandler is never called. I searched the Evernote SDK and found this kind of code in several places:

#if EN_PROGRESS_HANDLERS_ENABLED
    if (context.progress) {
        context.noteStore.uploadProgressHandler = context.progress;
    }
#endif

This seems to have something to do with it. But I must say that I'm not familiar with this #if...#endif syntax. I searched the sample project and couldn't find a place where the value for EN_PROGRESS_HANDLERS_ENABLED actually is set.

I thought that maybe the progressHandler only gets called when the progress takes a long enough time, so I recorded for a few minutes (4 MB) and uploaded that recording. The upload took a significantly longer time, but the progressHandler still didn't get called.

I searched around the internet for quite some time, but no luck. Why is the progress handler never called? How can I get the upload progress for an ENNote?

Here are the relevant parts of my code:

private func sendToEvernote() {
    let progressHandler: ENSessionProgressHandler = { progress in
        self.progressView.progress = Float(progress)
        self.progressView.alpha = 1
        print("Progress:", progress)  // Never gets printed
    }

    let completionHandler: ENSessionUploadNoteCompletionHandler = { (noteRef, error) in
        if let error = error { print(error.localizedDescription) }
        self.noteRef = noteRef
        self.progressView.alpha = 0
        self.showBanner(text: "Upload complete. Tap here to open in Evernote.")
    }

    do {
        let note = ENNote()
        let title = titleFromCurrentDate()
        let filename = title + ".m4a"

        note.title = title

        let data = try Data(contentsOf: audioURL)
        guard let resource = ENResource(data: data, mimeType: "audio/mp4a-latm", filename: filename)
            else {
                alert(title: "Export failed", message: "Audio file could not be attached to note.")
                return
        }

        note.add(resource)

        ENSession.shared.upload(note, policy: .create, to: nil, orReplace: nil, progress: progressHandler, completion: completionHandler)
    } catch {
        alert(title: "Export failed", message: "Audio file could not be converted to Data type.")
    }
}

Solution

  • The answer is simple and sad:

    "Deprecation of progress blocks - Unfortunately part of the changes have meant that progress blocks are no longer supported in the latest SDK."

    https://github.com/evernote/evernote-cloud-sdk-ios/blob/master/3.0_Release_Notes.md

    (Edit: As far as I understand it, that means that it is currently just not possible to get the upload progress.)


    Following Josh Caswell's answer, I added a preprocessor macro that set EN_PROGRESS_HANDLERS_ENABLED=1. (Although I'm using CocoaPods, this turned out to be the way it worked.) Doing this gave me the decisive clue. The console printed out: "Progress blocks have been deprecated". An internet search of this sentence led me to the release note page quoted above. I guess I should get into the habit of reading release notes.

    (By the way, the progressHandler now got called, but only once, with a progress of 1.0, even when recording for a longer period of time.)


    Without Josh Caswell's answer, I wouldn't have found out. Still I thought I should write my own answer, because for anyone having this problem, Josh's answer is not the answer to the question, although an important step on the way.


    Edit: Now I‘ve found out that there is already a corresponding issue on GitHub.