iOS download files and store to local drive

Hey guys,
i currently have a working code for downloading files from the web in iOS,
i am given a URL, and i use WWW to download the file. after the download is complete (the isDone of www is true), I save the file using C#'s System.IO.

my problem is i need to use a download method (or workaround) that allows me to securely download files. What would happen if the device sleeps during WWW? will the download resume when the device comes out of sleep or will the download fail?

I need a method to also show my progress in downloading the files. I have read that the WWW.progress method does not work for UNITY iOS.

The files i am currently downloading are 6 mb each, but it will increase up to 50mb to 100mb files, so I need a better alternative to WWW.

I am wondering if there is a native plugin/ code to be able to make iOS handle the download instead of using the Unity engine’s WWW class to handle it. or How i would be able to workaround this problem.


// Based on this:
// Downloading file via TcpClient from HTTP server - Stack Overflow

public class main : MonoBehaviour 
    string host = "";
    string uri = "/path/to/file/1920x1080.mp4";
    uint contentLength;
    int n = 0;
    int read = 0;

    NetworkStream networkStream; 
    FileStream fileStream;
    Socket client;

    // Use this for initialization
    void Start () 
       string query = "GET " + uri.Replace(" ", "%20") + " HTTP/1.1

" +
"Host: " + host + "
" +
"User-Agent: undefined
" +
"Connection: close

       Debug.Log (query);

       client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
       client.Connect(host, 80);   

       networkStream = new NetworkStream(client);

       var bytes = Encoding.Default.GetBytes(query);
       networkStream.Write(bytes, 0, bytes.Length);

       var bReader = new BinaryReader(networkStream, Encoding.Default);

       string response = "";
       string line;
       char c;

         line = "";
           c = '\u0000';
           while (true) 
             c = bReader.ReadChar();
                 if (c == '\r')
                 line += c;
           c = bReader.ReadChar();
           response += line + "

while (line.Length > 0);

       Debug.Log ( response );

       Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
       contentLength = uint.Parse(reContentLength.Match(response).Value);

       fileStream = new FileStream( Application.persistentDataPath + "/download.mp4", FileMode.Create);

    void Update()
       byte[] buffer = new byte[256 * 1024];

       if (n < contentLength) 
             if (networkStream.DataAvailable) 
               read = networkStream.Read(buffer, 0, buffer.Length);
               n += read;
               fileStream.Write(buffer, 0, read);
          Debug.Log ( "Downloaded: " + n + " of " + contentLength + " bytes ..." );



Hello, i have also had issues with WWW.progress initially, however it DOES work. Check out this example, it is working for me on unity 3.5.5 (pro). The mistake i made was checking WWW.progress inside Start(), but when i poll it in Update() it gives correct values. I have also read somewhere on the forum, there might have been a bug which requires you to poll WWW.isDone in order to update the WWW.progress value. However in my test with unity 3.5.5 this wasn’t necessary:

#pragma strict

var url = "";
var downloader: WWW = null;

function Start () 
	downloader = new WWW (url);
   	yield downloader;	

function Update () 
	if ( downloader != null )
		// value between 0 and 1
		print( "Download Progress: " + 100*downloader.progress + "% ..." );

Hi franky303!
What references are you using at the very top of the script? I think I’m missing one because “NetworkStream” is throwing errors.

Here is a nice plugin, its very easy to use. You can download any small or big file from the web.
Must see this plugin

For more details visit this blog