0

I have registered my file type and when I am trying to read the passed file, I always get an access denied exception. This is my current code:

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    var text = File.ReadAllText(url.Path); <- Exception here
    // todo: handle text
    return true;
}

On android I got it working with the ContentResolver:

using (var inputStream = ContentResolver.OpenInputStream(Intent.Data))
{
    using (StreamReader reader = new StreamReader(inputStream))
    {
        string text = reader.ReadToEnd();
        Clipboard.SetTextAsync(text);
    }
}

but I did not find an equivalent to read the passed file on iOS.

flayn
  • 5,272
  • 4
  • 48
  • 69

1 Answers1

3

Take a look at this thread: Handling application: openURL: sourceApplication: to open files in iOS app .

I convert objective-C to c# , you could try it in your project .

    public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
        {

            string urlPath = url.Path;

            NSData data = null;

            if (!NSFileManager.DefaultManager.IsReadableFile(urlPath)) {

                if (url.StartAccessingSecurityScopedResource())
                {
                    data = NSData.FromFile(urlPath);
                    url.StopAccessingSecurityScopedResource();
                }
            }
            else
            {
                data = NSData.FromFile(urlPath);
            }


            NSString str = NSString.FromData(data, NSStringEncoding.UTF8);
            return true;
        }
ColeX
  • 14,062
  • 5
  • 43
  • 240