Hello,
With MAUI, you could invoke platform code instead of DependencyService
, and use conditional compilation to target different platforms.
For example, I invoke the code in MainPage directly, please refer to the following code:
(You could also try Environment.SpecialFolder.MyDocuments
to get the Document path. Besides, you could try FileSystem.AppDataDirectory
to save the file in Library folder, see File system helpers - .NET MAUI | Microsoft Learn)
private void OnCounterClicked(object sender, EventArgs e)
{
#if IOS
string databasePath = this.GetLocalFilePath("OMSAnthroP.db3");// your GetLocalFilePath method
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "OMSAnthroP.db3");// you could call this to find Document folder
//var path = Path.Combine(FileSystem.AppDataDirectory, "OMSAnthroP.db3");
#endif
}
#if IOS
public string GetLocalFilePath(string filename)
{
string downloadPath;
NSFileManager fileManager = NSFileManager.DefaultManager;
NSUrl[] urls = fileManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User);
if (urls != null && urls.Length > 0 && urls[0] != null)
{
downloadPath = urls[0].Path ?? throw new Exception("El Path de la URL es nulo.");
}
else
{
throw new Exception("No se pudo obtener el directorio de documentos de la aplicación.");
}
return Path.Combine(downloadPath, filename);
}
#endif
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.