简体   繁体   English

如何从iOS中的流中接收数据(swift)

[英]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.

相关问题 如何快速(IOS 8)连接和发送/接收数据到蓝牙设备(OBDII)? - How to connect and send/receive data to bluetooth device (OBDII) in swift (IOS 8)? iOS(swift)如何从数据库中检索数据 - iOS(swift) How to retrieve data from database iOS / Swift:当应用程序处于后台时,无需推送通知即可接收来自数据流服务的回调 - iOS / Swift : Receive callbacks from data streaming service without push notifications while app is in background 如何通过iOS中的套接字从服务器接收数据? - How to receive data from server through socket in iOS? 如何在iOS中从服务器接收和存储二进制数据? - How to receive and store binary data from server in iOS? 来自Google驱动器Swift的iOS流视频 - iOS Stream video from Google drive Swift 如何快速播放来自 http 数据流的音频 - How to play audio from an http data stream in swift 如何使用事件通道将数据从 swift 流式传输到 flutter? - How to stream data from swift to flutter using event channel? 从GameCenter发送和接收数据:Swift - Send and receive data from GameCenter: Swift 我无法接收iOS swift 3的数据通知 - I am not able to receive data notification iOS swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM