簡體   English   中英

如何從 Swift 4.2 中的共享 Dropbox 鏈接讀取文本文件的內容(無需下載)?

[英]How do I read a text-file's content from it's shared Dropbox link in Swift 4.2 (without downloading)?

我很難弄清楚如何通過 Swift 4.2 從共享的 Dropbox 鏈接(無需下載)中獲取簡單的文本文件內容。

例如:

let url = URL(string: "https://www.dropbox.com/s/rokwv82h54ogwy1/test.txt?dl=0")!
// the dropbox link above is a shared link so anyone can view it
    
do {
                
    let content = try String(contentsOf: url)
    print("File Content:   \(content)")
} catch let error as NSError {
                
    print("\(error)")
}

當我運行此代碼時,我收到此錯誤:

錯誤域=NSCocoaErrorDomain Code=260 “文件“test.txt”無法打開,因為沒有這樣的文件。” (還有更多錯誤,但它很大)

誰能幫幫我? 謝謝。

還有更多的錯誤,但它相當大

不要剝離錯誤消息。 如果你不知道如何解決這個問題,你可能不知道要剝離什么來保持它的價值。

如何解決您的問題

  • Select 目標
  • 切換到簽名和功能選項卡
  • 應用沙盒-網絡- 啟用傳出連接(客戶端)

在此處輸入圖像描述

  • 將 URL ( dl=0 ) 更改為 ( dl=1 )
    • 0 = 顯示帶有預覽和下載鏈接的 web 頁面
    • 1 = 不顯示任何 web 頁面,只提供文件
let url = URL(string: "https://www.dropbox.com/s/rokwv82h54ogwy1/test.txt?dl=1")!
// Change dl=0 to dl=1                                                       ^
    
do {
    let content = try String(contentsOf: url)
    print("File Content: \(content)")
} catch let error as NSError {
    print("\(error)")
}

再次運行,你會得到:

File Content:   
This is a test. If you can read this, you have passed! :)

不要使用String(contentsOf: url) ,因為它不是異步的,它會阻塞主線程(UI)。

異步示例 - 假設您有一個帶有一個文本字段(標簽)的視圖 controller,並且您想在那里顯示文件內容:

import Cocoa

class ViewController: NSViewController {
    @IBOutlet var textField: NSTextField!
    
    override func viewWillAppear() {
        super.viewWillAppear()
        textField.stringValue = "Loading ..."
        loadRemoteFile()
    }
    
    func loadRemoteFile() {
        let url = URL(string: "https://www.dropbox.com/s/rokwv82h54ogwy1/test.txt?dl=1")!
        
        let task = URLSession.shared.dataTask(with: url) { data, _, error in
            // Following code is not called on the main thread. If we'd like to
            // modify UI elements, we have to dispatch our code on the main thread.
            // Hence the DispatchQueue.main.async {}.
            
            if let error = error {
                print("Failed with error: \(error)")
                DispatchQueue.main.async { self.textField.stringValue = "Failed" }
                return
            }

            guard let data = data,
                  let content = String(data: data, encoding: .utf8) else {
                print("Failed to decode data as an UTF-8 string")
                DispatchQueue.main.async { self.textField.stringValue = "Failed" }
                return
            }
            
            print("Content: \(content)")
            DispatchQueue.main.async { self.textField.stringValue = content }
        }
        
        // At this point, we have a task which will download the file, but the task
        // is not running. Every task is initially suspended.
        
        task.resume() // Start the background task
        
        // At this point, your program normally continues, because the download
        // is executed in the background (not on the main thread).
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM