简体   繁体   中英

MonoTouch iOS application can't find files in directory on device - works on simulator

If I run this code on the device, it returns "got it":

if (Directory.Exists (NSBundle.MainBundle.BundlePath + "/ARCSDev")) {
                Console.WriteLine ("got it");
            } else {
                Console.WriteLine ("can't find it");
            }

Which means the directory is in the main bundle.

I need to use that file in this method call:

private void generateChart (int chartNumber, string palette)
        {
            string filePath = NSBundle.MainBundle.BundlePath + "/ARCSDev";

            Console.WriteLine("Filepath - " + filePath);

            Loader loader = new Loader (filePath);
            loader.LoadChart (Convert.ToString (chartNumber));

The above code works fine on the simulator, but not on the device.

I get the following stack trace when the error occurs on the device:

System.InvalidOperationException: Operation is not valid due to the current state of the object
  at System.Linq.Enumerable.Max[TileIndexRecord] (IEnumerable`1 source, System.Func`2 selector) [0x00000] in <filename unknown>:0
  at MyCompany.Data.Arcs.Loader.ExtractWriteableBitmap (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette, Double dpi, MyCompany.Data.Arcs.Raschts.ChartIndexFile indexFile, MyCompany.Data.Arcs.Raschts.RasterChartFile chartFile) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:571
  at MyCompany.Data.Arcs.Loader.GetHiResImage (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:362
  at ARCSViewer.SecondViewController.generateChart (Int32 chartNumber, System.String palette) [0x0004e] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:118
  at ARCSViewer.SecondViewController.ViewDidAppear (Boolean animated) [0x00007] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:84
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at ARCSViewer.Application.Main (System.String[] args) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Main.cs:17

The directory contains unix executable files and radiance files.

Can anyone explain whats going on? The file is definitely in the right place (the bundle) as I've tested the code that checks for existence with other files that I know to be there as well..

It might be an issue of case sensitivity, the simulator is case-insensitive while the device is case sensitive. Check your files to see if you're accessing all of them with the right case (ie not only the directory).

The permissions on the devices are very different (far more restricted) from the simulator. There's a lot of place you do not have access for various reasons (eg changing some files would break the application digital signature which would disallow your application from running anymore).

Also if your application does not follow Apple guidelines on where (and when) to store data your application will be rejected (if you target the appstore).

There's a nice article from Xamarin on how to work with the iOS file system.

I solved the problem, several parts of my application use code like below:

fs = new FileStream(fileName, FileMode.Open);

I needed to change every occurrence of FileStream() to the following:

fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

As my files are in the bundle and don't need to make use of write operations, this limits the file stream to only read operations and allowed the files to be read in properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM