2016-08-05 73 views
0

Ich weiß, wie eine Remote-URL in Swift bekommtWie in Remote-Server-Datei in Dokumentenverzeichnis kopieren swift

let remoteURL = NSURL(string: "https://myserver/file.txt")! 

Ich weiß, wie

let localURL = NSURL(fileURLWithPath: documentsFolder + "/my_local_file.txt") 

und leider eine lokale URL in Swift bekommen dies nicht funktioniert,

NSFileManager.defaultManager().copyItemAtURL(remoteURL, toURL: localURL) 

mit dem folgenden Fehler

The file “file.txt” couldn’t be opened because URL type https isn’t supported. 

Gibt es eine Möglichkeit, dies zu tun?

Antwort

2

können Sie NSURLSessionDownloadTask verwenden, um die Datei herunterzuladen:

func downloadFile(url: URL) { 
    let downloadRequest = URLRequest(url: url) 
    URLSession.shared.downloadTask(with: downloadRequest) { location, response, error in 
     // To do check resoponse before saving 
     guard let tempLocation = location where error == nil else { return } 
     let documentDirectory = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask).last 
     do { 
      let fullURL = try documentDirectory?.appendingPathComponent((response?.suggestedFilename!)!) 
      try FileManager.default.moveItem(at: tempLocation, to: fullURL!) 
      print("saved at \(fullURL) ") 
     } catch NSCocoaError.fileReadNoSuchFileError { 
      print("No such file") 
     } catch { 
      // other errors 
      print("Error downloading file : \(error)") 
     } 
    }.resume() 
} 

let stringURL = "https://wordpress.org/plugins/about/readme.txt" 
downloadImage(url: URL(string: stringURL)!) 

Update: SWIFT 2.2

func downloadFile(url: NSURL) { 
    let downloadRequest = NSURLRequest(URL: url) 
    NSURLSession.sharedSession().downloadTaskWithRequest(downloadRequest){ (location, response, error) in 

     guard let tempLocation = location where error == nil else { return } 
     let documentDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first 
     let fullURL = documentDirectory?.URLByAppendingPathComponent((response?.suggestedFilename)!) 

     do { 
      try NSFileManager.defaultManager().moveItemAtURL(tempLocation, toURL: fullURL!) 
     } catch NSCocoaError.FileReadNoSuchFileError { 
      print("No such file") 
     } catch { 
      print("Error downloading file : \(error)") 
     } 

     }.resume() 
} 

let stringURL = "https://wordpress.org/plugins/about/readme.txt" 
let url = NSURL.init(string: stringURL) 
downloadFile(url!) 
+0

ist das Swift 2.x? Es gibt mir mehrere Fehler ... – Nicholas

+0

Es ist Swift 3, ich werde es in einer Minute ändern. – Khundragpan

+0

Sorry, ich bin nicht zu Swift 3 gezogen, leider weiß ich, dass ich sollte. Bitte halten Swift 3-Version für die Zukunft :) – Nicholas

1

Sie sollten es zuerst herunterladen und dann in einer lokalen Datei speichern.

Code-Beispiel finden Sie hier: (mit AFNetworking)

How I properly setup an AFNetworking Get Request?

+0

alles sw ift verwandt? – Nicholas

+0

Es ist nur ein einfaches Stück Code. Sie können Swift ganz einfach übersetzen. –

+0

alles andere, ohne dass eine externe Bibliothek importiert werden muss? Gibt es im reinen Swift mit Apple Frameworks keinen einfachen Weg? – Nicholas