0

How to download a file over smb in Swift (tvOS)? NetFS and Cooca are not available for tvOS and when I'm using URLSession dataTask "smb://xxx.xxx.xxx.xx/share/test.txt" Swift tells me that smb is not a valid url.

Is there any solution to download a file over smb for tvOS?

Tried it with this code:

 let url = self.smbManager.getFullUrl(fileName: self.file.name)
 let request: NSURLRequest = NSURLRequest(url: url!)
 let session = URLSession.shared
 session.dataTask(with: request as URLRequest) { data, response, error in
     print(data)
 }.resume()

Error is: finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=smb://xxx.xxx.xxx.xx/share/test.txt, NSErrorFailingURLKey=smb://xxx.xxx.xxx.xx/share/test.txt, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask .<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<1>, NSUnderlyingError=0x2813a7c60 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}

Thanks.

Leahpar
  • 583
  • 1
  • 4
  • 12
  • There have already been some research about using samba in iOS : https://stackoverflow.com/questions/8032314/smb-samba-support-on-ios – Ptit Xav Jan 04 '21 at 17:38
  • Yeah, but iOS do have NetFS what tvOS do not have and so on... – Leahpar Jan 04 '21 at 17:44

1 Answers1

0

Solution for smb:

let url = NSURL(string: "smb://xxx.xxx.xxx.xx/share/test.txt")
let request: NSURLRequest = NSURLRequest(URL: url!)
let session = NSURLSession.sharedSession()   
session.dataTaskWithRequest(request) { (data, response, error) -> Void in                                  print("Response")    
let reply = NSString(data: data!, encoding: NSUTF8StringEncoding) 

print(reply)
    
}

URLSession is designed to make network transfers as easy as possible, and a great example of that is its `downloadTask() method. This fetches the contents of a URL you specify, saves it to a local file, then calls a completion handler so you can manipulate the file – all in one.

To demonstrate this, here’s some code to download the source code to the apple.com homepage:

let url = URL(string: "https://www.apple.com")!

let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
    if let localURL = localURL {
        if let string = try? String(contentsOf: localURL) {
            print(string)
        }
    }
}

task.resume()

There are a few important things to note in there:

  1. Your completion handler gets called with a local URL, which is where the data was saved locally. This is optional, so you need to unwrap it carefully.
  2. If something went wrong – e.g. if the network was down – then you’ll get an error passed to you explaining what happened.
  3. When you have created your download task you should call resume() on it to make it happen.
  4. You don’t need to worry about storing the download task somewhere while it happens – it’s being tracked by the shared URLSession on your behalf.
Grenoblois
  • 503
  • 1
  • 5
  • 19