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.
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;
do
{
line = "";
c = '\u0000';
while (true)
{
c = bReader.ReadChar();
if (c == '\r')
break;
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 ..." );
}
else
{
fileStream.Flush();
fileStream.Close();
client.Close();
}
}
}
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 = "http://www.blabla.com/somebigfile.mp4";
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 + "% ..." );
}
}