简体   繁体   中英

Why do I get a permission error in the OpenUrl function on an iOS device when trying to read in the file that has been sent to my app?

I have my Info.plist file setup to handle the file type I want. When I download the file in Safari, and pick my App from the Open In, the file does get sent to my app and the OpenUrl function is called. In the Simulator, I am able to open the file using FileStream and process it without any problem. On the real device, I get an error that says something like: Accesss to the path "/private/var/mobile/Containers/Data/Application/{KEY}/Documents/Inbox/file.ext is denied.

Here is my OpenUrl function:

public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        try
        {
            System.Console.WriteLine("OpenURL");
            var fileName = url.AbsoluteUrl.LastPathComponent;
            var newPath = Path.Combine(bookStorage, fileName);

            var input = new System.IO.FileStream(url.Path, FileMode.Open);
            var output = new System.IO.FileStream(newPath, FileMode.Create);
            input.CopyTo(output);
            output.Close();
            input.Close();
            var epub = new EpubParser(bookStorage, fileName);
            var publication = new Publication(epub.getTitle(), fileName);
            var id = epub.getId();
            if (id != null)
            {
                publication.Id = id;
            }
            publication.Picture = epub.getImage();
            var publications = new Publications();
            publications.AddPublication(publication);

            return true;
        }
        catch (Exception e)
        {
            var navController = (UINavigationController)this.Window.RootViewController;
            var controller = navController.ViewControllers[0];
            AlertView.Show(e.Message, controller);
            return false;
        }

    }

Basically all I wanted was to copy the file to another location to keep it stored. So I used File.Copy instead and it worked! Apparently FileStream even with just the Open permissions set, still requires some type of write access to the folder.

var fileName = url.AbsoluteUrl.LastPathComponent;
var newPath = Path.Combine(bookStorage, fileName);
File.Copy(url.Path, newPath);

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