我的應用程序記錄音頻,然後創建一個ENNote,並將生成的音頻文件作爲資源(以及當前的日期和時間作爲標題),並將此音符上傳到Evernote。上傳完成後,用戶已收到通知。現在我也想顯示上傳進度。所以我使用這個功能:印象筆記:我如何獲得筆記的上傳進度?
ENSession.shared.upload(note, policy: .create, to: nil, orReplace: nil, progress: progressHandler, completion: completionHandler)
一切正常,除了的progressHandler永遠不會被調用。 我搜索Evernote的SDK,並發現這種代碼在幾個地方:
#if EN_PROGRESS_HANDLERS_ENABLED
if (context.progress) {
context.noteStore.uploadProgressHandler = context.progress;
}
#endif
這似乎有什麼用它做。但是我必須說我對這個#if ...#endif語法不熟悉。我搜索了示例項目,並且找不到實際設置了EN_PROGRESS_HANDLERS_ENABLED值的地方。
我認爲progressHandler只有在進程需要足夠長的時間纔會被調用,所以我記錄了幾分鐘(4 MB)並上傳了該記錄。上傳花費了更長的時間,但progressHandler仍然沒有被調用。
我在互聯網上搜索了很長時間,但沒有運氣。 爲什麼進程處理程序從不調用?我如何獲得ENNote的上傳進度?
這裏是我的代碼的相關部分:
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.")
}
}
對,很高興你發佈了更完整的解決方案。 –