-2

I have this code:

var myDocuments = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Shared");
_rootPath = Path.Combine(myDocuments, "VisitsRota.MacOS");
_stylesPath = Path.Combine(_rootPath, "Styles");
_vrrDataFile = Path.Combine(_rootPath, "VisitsRotaData.xml");

// Read in the required data
vrrData = DeserializeFromXml<VisitsRotaData>(_vrrDataFile);

// Build list of month names. Should this property also have a private counterpart?
ListMonths = DateTimeFormatInfo.CurrentInfo.MonthNames.TakeWhile(m => m != String.Empty).ToList();

// Select the current month in the list
ListMonthsSelectedItem = DateTime.Now.ToString("MMMM");

string[] stylesArray = Directory.GetFiles(_stylesPath, "ElderlyInfirm-Schedule-*.xsl")
                          .Select(file => Path.GetFileName(file)).ToArray<string>();

On the Mac it runs as expect. But on the iOS simulator (which I am new to using) it raises an exception:

enter image description here

System.IO.DirectoryNotFoundException: Could not find a part of the path '/Users/xxxxxxx/Library/Developer/CoreSimulator/Devices/B812608B-8131-4386-B189-C646684A8965/data/Containers/Data/Application/EF23B73D-8D38-4F41-995E-FCAE13AE3035/Shared/VisitsRota.MacOS/Styles'.

How should I be able to replicate testing on my iOS build? if I use literal paths instead of Path.Combine etc. I do not have a problem.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • I am going to check this: https://lostmoa.com/blog/AccessApplicationFilesOniOSSimulator/ – Andrew Truckle Feb 14 '21 at 12:15
  • 1
    how are these files being deployed with your app? – Jason Feb 14 '21 at 12:35
  • @Jason At the moment I have not built a deployment package as I have not found a tutorial for Visual Studio for Mac. At the moment I was just copying the data folder to the right place on the Mac for testing. Deployment is an issue I must also address. – Andrew Truckle Feb 14 '21 at 12:38
  • 1
    Well that's your answer. You have separately deployed the files on your Mac, and haven't done that on iOS, so iOS can't find the files because they don't exist. – Jason Feb 14 '21 at 12:43
  • @Jason Yes, but I am only debugging in the VS IDE. I will try that link and copy the files to that weird folder from the exception. – Andrew Truckle Feb 14 '21 at 12:45
  • 1
    the iOS simulator uses the same filesystem structure as an iOS device, just replicated locally on your Mac. It cannot read arbitrary file locations from your Mac, it can only access it's own file system, and abides by the same sandbox restrictions as an iOS device. – Jason Feb 14 '21 at 12:50
  • @Jason Can't follow the tutorial I linked to. `NSHomeDirectory` does not exist and no `using` is suggested. – Andrew Truckle Feb 14 '21 at 12:52

1 Answers1

0

I was able to resolve this. My related question / answer about resources helped.

In the comments to this question I was asked:

How are these files being deployed with your app?

That was the key!

I added a constructor to the AppDelegate class:

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public AppDelegate()
    {
        InstallApplicationFiles();
    }

The linked Q & A has more info. By getting the app to copy the default files from the resources the issue of paths with the iOS simulator is resolved.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164