2

I am using Xamarin.Forms and written the code to download the file for the iOS platform. It is downloading the file successfully without any error. But after downloading it, I am not able to find the downloaded file in my apple device.

During debugging I found that it is showing

/var/mobile/Containers/Data/Application/1234567A-B8CD-9EF9-C850-9G73587DC7C/Documents/XF_Downloads/hausmann_abcd.jpg

path. So at which location file get saved? below is the image for the same.

enter image description here

I have written below code for this

public class IosDownloader : IDownloader
{
    public event EventHandler<DownloadEventArgs> OnFileDownloaded;

    public void DownloadFile(string url, string folder)
    {
        string pathToNewFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), folder);
        Directory.CreateDirectory(pathToNewFolder);

        try
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
            webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
        }
        catch (Exception ex)
        {
            if (OnFileDownloaded != null)
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
        }
    }

    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            if (OnFileDownloaded != null)
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
        }
        else
        {
            if (OnFileDownloaded != null)
                OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
        }
    }
}
James Z
  • 11,838
  • 10
  • 25
  • 41
  • "I am not able to find the downloaded file" - how exactly are you trying to do this? iOS does not supply tools for the user to explore the device file system – Jason Jan 03 '20 at 14:08
  • I have written the above code to save any type of file for iOS. So from which path I can view those files.? – V-Vek P-Tel Jan 06 '20 at 09:56

2 Answers2

3

When a file is saved on an application it is a temporary url within the app's sandbox. To make this image file be publicly accessible through Apple's Photos, You'll have to use native code to do a request to add a new PHImageAsset to the photos library.

In forms you would need to access the native frameworks and therefore run native code. There are plenty of examples of doing this online if you don't know how to already. But here is an introduction if you want to run native code within a shared code framework. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

Here is a sample of taking that temp file URL and saving it to Photos Framework once you can code w/ native Xamarin.iOS frameworks:

    public void AddImageUrlToPhotosLibrary(string urlToSaveToPhotos)
    {
        PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => {

            //This is bound to native https://developer.apple.com/documentation/photokit/phassetchangerequest/1624060-creationrequestforasset
            //Parameter is an NSURL of path to image.
            PHAssetChangeRequest.FromImage(new NSUrl(urlToSaveToPhotos));

        }, (completed, error) => {

            if (completed)
            {
                if (error != null)
                {
                    Console.WriteLine($"Failed saving image asset {error.LocalizedDescription}");
                } else
                {
                    Console.WriteLine($"Successfully saved image to photos library.");
                }
            }
        });
    }
NSGangster
  • 2,307
  • 8
  • 22
  • Thanks for your answer. It's great for photos but, what about the other files like .pdf or .docx or any other file format? What should I have to do to store files with any extension and check/list/view it on my iPhone? – V-Vek P-Tel Jan 06 '20 at 05:05
  • 1
    Not unless you have a "file viewer" or an app that can support opening that file. I don't think there is currently a centralized place to store files outside your apps – NSGangster Jan 07 '20 at 20:05
1

I am not able to find the downloaded file in my apple device.

If you use a simulator, you can directly find the folder in the mac by entering the path in the

your mac --> select Finder --> Then open the Go menu --> Click Go to Folder as I described in this thread:

Where can I find the MyDocuments folder on iPhone Simulator?

If you installed the app in a real device. You can browse the file in

Xcode --> Devices and Simulators --> download container as described in this thread:

Browse the files created on a device by the iOS application I'm developing, after downloading the container, you can right click it and choose Show Package Contents. Then you can see the folders.

image

You can also access the file by the path in the project.

Jack Hua
  • 14,231
  • 1
  • 5
  • 19
  • I have installed the app in a real device. As you suggested I can browse the file successfully. Thanks for that. But the thing is my application is having real-time chat and file sharing option. So the user wants to check the downloaded file in his iPhone device in realtime. He can not connect his iPhone to Xcode then view the files. I think this option is for developers. What should I do if actual users want to see those files in their iPhone? – V-Vek P-Tel Jan 06 '20 at 05:25
  • Look at the answer in this [thread](https://forums.xamarin.com/discussion/47357/how-to-access-any-file-from-a-given-path-available-in-filesystem). – Jack Hua Jan 06 '20 at 05:43
  • The files are of different types like .png, .jpeg, .pdf or any other file format, so what about that? How should I check/list/view it on my iPhone? – V-Vek P-Tel Jan 06 '20 at 05:48
  • You can save files with different types into different sub folders. You should know the type of file first, and then you can read them correctly. – Jack Hua Jan 06 '20 at 05:54