SimonSimCity / Xamarin-CrossDownloadManager

A cross platform download manager for Xamarin
MIT License
149 stars 68 forks source link

PathNameForDownloadedFile not executing #124

Closed youceflebid closed 4 years ago

youceflebid commented 4 years ago

Hello,

First, I would like to thank you for your plugin. I works perfectly on Android.

But I am having a hard time making it work in iOS. I have an old Iphone 5 with iOS 10.3.4.

The problem, exactly, is that the function i am setting up for PathNameForDownloadedFile is not executing when i call CreateDownloadFile.

The download is initialised but the function is not called.

Here is a code sample :

public class DownloadService : IDownloadService
    {
        public string Download(string link)
        {
            string path = "";
            CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<IDownloadFile, string>(f =>
            {
                var splits = f.Url.Split('/');
                string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(splits[8]));
                var extension = decoded.Split('.');
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + decoded.Substring(0, decoded.LastIndexOf('-', decoded.Length - 1)) + "." + extension[extension.Length - 1];
                path = Path.Combine(NSSearchPath.GetDirectories(NSSearchPathDirectory.AllLibrariesDirectory, NSSearchPathDomain.User)[0], fileName);
                return Path.Combine(NSSearchPath.GetDirectories(NSSearchPathDirectory.AllLibrariesDirectory, NSSearchPathDomain.User)[0], fileName);
            });
            var downloadManager = CrossDownloadManager.Current;
            var headers = new Dictionary<string, string>()
            {
                {"Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(App.Username + ":" + App.Password))}
            };
            var file = downloadManager.CreateDownloadFile(link, headers);
            downloadManager.Start(file);
            while(path == "") { }
            return path;
        }
    }

Thank you for your help.

SimonSimCity commented 4 years ago

The problem, exactly, is that the function i am setting up for PathNameForDownloadedFile is not executing when i call CreateDownloadFile.

The download is initialised but the function is not called.

True, and this is expected. Since I had to unify the way iOS and Android do the download of files, I had to do it the following:

iOS requires the localtion for the final path first after the download is done, Android requires it when it starts the download. This requires you to create a function which - whenever called - can generate the path based on the given file-uri.

The data of what you have available there might vary because both the solution for iOS and the one for Android will continue downloading the file in the background when the app (which includes this plugin) crashes. The internal download managers will then start up this package to finish the post-download hook - which on Android isn't much, but on iOS requires me to copy the file to a different - the final - location. Hope this clears it up.