[英]How to receive Data from a stream in iOS (swift)
I am trying to get data from a stream in swift. 我试图从swift中获取流中的数据。 I know how to handle this in Android, but I dont know what to do in iOS.
我知道如何在Android中处理这个问题,但我不知道在iOS中该怎么做。 In Android I do the following to read data from my server an save it as file to my device:
在Android中,我执行以下操作从我的服务器读取数据并将其作为文件保存到我的设备:
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
input = connection.getInputStream();
output = new FileOutputStream(context.getFileStreamPath(filename));
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
File file = context.getFileStreamPath(filename);
How can I do the same with swift? 我怎么能用swift做同样的事情? I am using Alamofire for other requests.
我正在使用Alamofire进行其他请求。
best regards 最好的祝福
Almofire provides methods to download streams directly. Almofire提供直接下载流的方法。 This is an example mentioned in the Almofire github page
这是Almofire github页面中提到的一个例子
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, "https://httpbin.org/stream/100", destination: destination)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.