Xamarin 中的下载路径 iOS

Download path in Xamarin iOS

我正在使用 Xamarin.Forms 并编写代码来下载 iOS 的文件 平台。它正在成功下载文件,没有任何错误。但是下载后,我的苹果设备上找不到下载的文件。

在调试过程中我发现它显示

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

路径。那么文件保存在哪个位置?下面是相同的图像。

我为此编写了以下代码

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));
        }
    }
}

当文件保存在应用程序中时,它是应用程序沙箱中的临时 url。要使此图像文件可通过 Apple 的 Photos 公开访问,您必须使用本机代码来请求将新的 PHImageAsset 添加到照片库。

在表单中,您需要访问本机框架,因此 运行 本机代码。如果您还不知道如何操作,网上有很多这样的例子。但是,如果您想 运行 在共享代码框架内使用本机代码,这里有一个介绍。 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

这是获取临时文件 URL 并将其保存到 Photos 框架的示例,一旦您可以使用本机 Xamarin.iOS 框架进行编码:

    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.");
                }
            }
        });
    }

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

如果你使用模拟器,你可以在

中输入路径直接找到mac中的文件夹

your mac --> select Finder --> 然后打开 Go 菜单 --> 点击 Go to Folder 正如我在这个帖子中所描述的:

如果您在 真实设备 上安装了该应用程序。您可以在

中浏览文件

Xcode --> Devices and Simulators --> download container 如本帖所述:

Browse the files created on a device by the iOS application I'm developing,下载容器后,右击选择Show Package Contents即可。然后就可以看到文件夹了。

您也可以通过项目中的路径访问该文件