在Swift中使用NSURLSession下载文件 [英] Download a file with NSURLSession in Swift

查看:138
本文介绍了在Swift中使用NSURLSession下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有两个问题,首先我不能用一个swift项目设置NSURLSessionDownloadDelegate,编译器说

i have like 2 problems here , first i cant set NSURLSessionDownloadDelegate with a swift project, compiler says

Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate'

第二个问题是我无法找到NSURLSession方法来下载一个简单的文件

Second problem is i cant find NSURLSession methods to download a simple file

这是我用来下载简单文件的方式

here is the way i use to download the simple file

    var url:NSURL = NSURL.URLWithString(fileURL)
    var request:NSURLRequest = NSURLRequest(URL: url)
    var downloadTask:NSURLSessionDownloadTask = sessionManager.downloadTaskWithRequest(request)
    downloadTask.resume()

这些是我想在swift中制作的方法

and these are the methods i want to make in swift

URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

..

URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

..

URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

..
如果有新方法可以使用NSURLSession i下载文件我想知道,并且在NSURLSession中有什么新的快速

.. if there is a new way to download files with NSURLSession i would like to know , and whats new in NSURLSession in swift

推荐答案

我现在正处于一个带有后台下载管理器的项目中,以下是一些事情,我是如何解决的:

I am at the moment on a project with a background Download Manager, and here are a few things, how I solved that:

如果您使用 NSURLSessionDownloadDelegate ,则需要实现以下方法:

if you are using the NSURLSessionDownloadDelegate you need to implement the following methods:

func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) 

func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) 

func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) 

我通过此次通话完成了此操作:

I have done this with this call:

var session:NSURLSession!


    var sessionConfiguration:NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.company")
    sessionConfiguration.HTTPMaximumConnectionsPerHost = 5

    self.session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)

//下载

var downloadTask:NSURLSessionDownloadTask = self.session.downloadTaskWithURL(NSURL.URLWithString("urlfromyourfile"))
downloadTask.resume()

//出错:

func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didCompleteWithError error: NSError!) {

    if(error != nil) {

        println("Download completed with error: \(error.localizedDescription)");

    } else {

        println("Download finished successfully");

    }

}

在这里你找到一个很好的教程(我使用了很多来自该教程的代码并用swift编写了新的代码)

Here you find a good tutorial (i used lots of code from that tutorial and wrote it new with swift)

http://www.appcoda.com/background-transfer-service-ios7/

这篇关于在Swift中使用NSURLSession下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆