应用程序重新启动时未从文档文件夹加载 PDF iOS

PDF not loading from documents folder on App relaunch iOS

我有一个从服务器下载并保存的 pdf。接下来,我从 UIWebView 内的文件路径打开文件。这在我第一次启动该应用程序时有效。当我再次重新启动应用程序时,即使文件路径相同,文档也打不开。另外,该文档确实存在于应用程序的文档文件夹中。

我正在做类似的事情:-

SaveToFolder.cs

var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), fileName);

using (FileStream destinationStream = File.Create(filePath))
{
     await documentStream.CopyToAsync(destinationStream);
}

第一次保存文档后的文件路径:-

 /var/mobile/Containers/Data/Application/C3EA2325-81CA-4EC9-8C03-479ACF7EE330/Documents/Insufficiency.pdf

应用重新启动时的文件路径

/var/mobile/Containers/Data/Application/C3EA2325-81CA-4EC9-8C03-479ACF7EE330/Documents/Insufficiency.pdf  

我是不是做错了什么?

我在 iOS 中创建了一个文件用于读写文件。请查看iOS

using System;
using Xamarin.Forms;
using FileReader.iOS;
using System.IO;
using FileReader;
using Foundation;
using System.Linq;
using System.Threading.Tasks;

[assembly: Dependency(typeof(SaveAndLoadiOS))]

namespace FileReader.iOS
{
    public class SaveAndLoadiOS : LoadAndSave
    {
        public static string DocumentPath
        {
            get
            {
                var documentURL = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User).Last();
                return documentURL.Path;
            }
        }

        public string CreatePath(string fileName)
        {
            return Path.Combine(DocumentPath, fileName);
        }

        public async Task SaveTextAsync(string fileName, string text)
        {
            string path = CreatePath(fileName);
            if (IsFileExits(fileName))
            {
                File.Delete(path);
            }
            using (StreamWriter sw = File.CreateText(path))
                await sw.WriteAsync(text);  
        }

        public async Task<string>  LaodTextAsync(string fileName)
        {
            string path = CreatePath(fileName);
            using (StreamReader sr = File.OpenText(path))
                return await sr.ReadToEndAsync();
        }

        public bool IsFileExits(string fileName)
        {
            return File.Exists (CreatePath(fileName));
        }
    }
}

为了从我的 .CS class(ContentPage 的子class)读取,下面是代码

var tempFileService =  DependencyService.Get<LoadAndSave>();
var itemFile = await tempFileService.LaodTextAsync(tempFile.StoredFileName);
var rootobject = JsonConvert.DeserializeObject<Rootobject>(itemFile);

其中 LoadAndSave 是一个接口,如下所示

using System;
using System.Threading.Tasks;

namespace FileReader
{
    public interface LoadAndSave
    {
        Task SaveTextAsync(string fileName, string text);
        Task<string> LaodTextAsync(string fileName);
        bool IsFileExits(string fileName);
    }
}

希望对您有所帮助。

我 运行 前一段时间遇到了同样的问题。可以参考

根据回答

You shouldn't store raw file paths for persistence (or if you do, know that the root can move on you). A better practice would be to only store the relative part of the path and always attach it to the current "root" path in question (particularly if you might be sharing data across devices as with iCloud).

也许你的根也在改变。您可以更改您的方法并将带有默认路径的文件名附加到您的文档文件夹,就像在 Xamarin 中一样:-

var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); filePath = docsPath +"/" + "Insuffeciency.pdf";

此外,考虑在保存文件时将 Personal 文件夹更改为 MyDocuments 文件夹。