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

应用程序重新启动iOS时未从文档文件夹加载PDF,ios,pdf,xamarin,uiwebview,nsdocumentdirectory,Ios,Pdf,Xamarin,Uiwebview,Nsdocumentdirectory,我有一个从服务器下载并保存的pdf。接下来,我从UIWebView中的文件路径打开该文件。这在我第一次启动应用程序时起作用。当我再次启动应用程序时,即使文件路径相同,文档也不会打开。此外,该文档确实存在于应用程序的文档文件夹中 我正在做一些类似于:- SaveToFolder.cs var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), fileName); using

我有一个从服务器下载并保存的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));
        }
    }
}
使用系统;
使用Xamarin.Forms;
使用FileReader.iOS;
使用System.IO;
使用文件阅读器;
使用基础;
使用System.Linq;
使用System.Threading.Tasks;
[程序集:依赖项(typeof(SaveAndLoadiOS))]
命名空间FileReader.iOS
{
公共类SaveAndLoadiOS:LoadAndSave
{
公共静态字符串文档路径
{
得到
{
var documentURL=NSFileManager.DefaultManager.getURL(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomain.User).Last();
返回documentURL.Path;
}
}
公共字符串CreatePath(字符串文件名)
{
返回Path.Combine(DocumentPath,fileName);
}
公共异步任务SaveTextAsync(字符串文件名,字符串文本)
{
字符串路径=CreatePath(文件名);
if(IsFileExits(fileName))
{
删除(路径);
}
使用(StreamWriter sw=File.CreateText(路径))
wait sw.WriteAsync(文本);
}
公共异步任务LaodTextAsync(字符串文件名)
{
字符串路径=CreatePath(文件名);
使用(StreamReader sr=File.OpenText(路径))
return wait sr.ReadToEndAsync();
}
public bool IsFileExits(字符串文件名)
{
return File.Exists(CreatePath(fileName));
}
}
}
为了阅读my.CS类(ContentPage的子类),下面是代码

var tempFileService =  DependencyService.Get<LoadAndSave>();
var itemFile = await tempFileService.LaodTextAsync(tempFile.StoredFileName);
var rootobject = JsonConvert.DeserializeObject<Rootobject>(itemFile);
var tempFileService=DependencyService.Get(); var itemFile=await tempFileService.LaodTextAsync(tempFile.StoredFileName); var rootobject=JsonConvert.DeserializeObject(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);
    }
}
使用系统;
使用System.Threading.Tasks;
命名空间文件读取器
{
公共接口加载和保存
{
任务SaveTextAsync(字符串文件名、字符串文本);
任务LaodTextAsync(字符串文件名);
bool IsFileExits(字符串文件名);
}
}

希望有帮助。

我在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));
        }
    }
}
使用系统;
使用Xamarin.Forms;
使用FileReader.iOS;
使用System.IO;
使用文件阅读器;
使用基础;
使用System.Linq;
使用System.Threading.Tasks;
[程序集:依赖项(typeof(SaveAndLoadiOS))]
命名空间FileReader.iOS
{
公共类SaveAndLoadiOS:LoadAndSave
{
公共静态字符串文档路径
{
得到
{
var documentURL=NSFileManager.DefaultManager.getURL(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomain.User).Last();
返回documentURL.Path;
}
}
公共字符串CreatePath(字符串文件名)
{
返回Path.Combine(DocumentPath,fileName);
}
公共异步任务SaveTextAsync(字符串文件名,字符串文本)
{
字符串路径=CreatePath(文件名);
if(IsFileExits(fileName))
{
删除(路径);
}
使用(StreamWriter sw=File.CreateText(路径))
wait sw.WriteAsync(文本);
}
公共异步任务LaodTextAsync(字符串文件名)
{
字符串路径=CreatePath(文件名);
使用(StreamReader sr=File.OpenText(路径))
return wait sr.ReadToEndAsync();
}
public bool IsFileExits(字符串文件名)
{
return File.Exists(CreatePath(fileName));
}
}
}
为了阅读my.CS类(ContentPage的子类),下面是代码

var tempFileService =  DependencyService.Get<LoadAndSave>();
var itemFile = await tempFileService.LaodTextAsync(tempFile.StoredFileName);
var rootobject = JsonConvert.DeserializeObject<Rootobject>(itemFile);
var tempFileService=DependencyService.Get(); var itemFile=await tempFileService.LaodTextAsync(tempFile.StoredFileName); var rootobject=JsonConvert.DeserializeObject(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);
    }
}
使用系统;
使用System.Threading.Tasks;
命名空间文件读取器
{
公共接口加载和保存
{
任务SaveTextAsync(字符串文件名、字符串文本);
任务LaodTextAsync(字符串文件名);
bool IsFileExits(字符串文件名);
}
}

希望能有所帮助。

我不久前遇到过同样的问题。你可以参考

根据答案

您不应该为持久性存储原始文件路径(或者,如果您存储了原始文件路径,请知道根目录可以在您身上移动)。更好的做法是只存储路径的相对部分,并始终将其附加到有问题的当前“根”路径(特别是如果您可能像iCloud那样在设备间共享数据)。

也许你的根也在改变。您可以更改方法并将带有默认路径的文件名附加到documents文件夹,如Xamarin中所示:-

var docsPath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
filePath=docsPath+“/”+“inufficiency.pdf”


也可以考虑在保存文件时将<代码>个人<代码>文件夹改为<代码> MyDebug 文件夹。

我刚才遇到了同样的问题。你可以参考

根据答案

您不应该为持久性存储原始文件路径(或者,如果您存储了原始文件路径,请知道根目录可以在您身上移动)。更好的做法是只存储相对的