Studyxnet / FilePicker-Plugin-for-Xamarin-and-Windows

FilePicker Plugin for Xamarin and Windows
MIT License
74 stars 52 forks source link

iOS: iPad: UIDocumentMenuViewController is not visible / not selectable. #23

Closed elmernocon closed 7 years ago

elmernocon commented 7 years ago

Xam.Plugin.FilePicker Version (NuGet + my modifications) Xamarin Forms Version 2.3.3.180 Xamarin.iOS Version 10.3

Before calling 'FileData fileData = await CrossFilePicker.Current.PickFile();' test1

After calling 'FileData fileData = await CrossFilePicker.Current.PickFile();' When clicking on anything it will treat it as a UIDocumentMenuViewController.WasCancelled. test2

My Modifications (ActiveViewController & OnCancelled return null.)

using ...

namespace Plugin.FilePicker
{
    public class FilePickerImplementation : NSObject, IUIDocumentMenuDelegate, IFilePicker
    {
        ...

        private void OnCancelled ()
        {
            Handler?.Invoke(null, null);
        }

        public void DidPickDocumentPicker (UIDocumentMenuViewController documentMenu, UIDocumentPickerViewController documentPicker)
        {
            ...
            documentPicker.WasCancelled += DocumentPicker_WasCancelled;

            // UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(documentPicker, true, null);
            GetActiveViewController().PresentViewController(documentPicker, true, null);
        }

        ...

        private void DocumentPicker_WasCancelled (object sender, EventArgs e)
        {
            OnCancelled();
        }

        ...

        private Task<FileData> TakeMediaAsync ()
        {
            ...

            // UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(importMenu, true, null);
            GetActiveViewController().PresentViewController(importMenu, true, null);

            ...

            if (presPopover != null)
            {
                //presPopover.SourceView = UIApplication.SharedApplication.KeyWindow.View;
                presPopover.SourceView = GetActiveViewController().View;
                ...
            }

            ...

            Handler = (s, e) =>
            {
                ...

                if (e != null)
                    tcs.SetResult(new FileData(e.FilePath, e.FileName, () => File.OpenRead(e.FilePath)));
                else
                    tcs.SetResult(null);
            };

            ...
        }

        public void WasCancelled (UIDocumentMenuViewController documentMenu)
        {
            OnCancelled();
        }

        ...

        public static UIViewController GetActiveViewController ()
        {
            UIViewController vc = UIApplication.SharedApplication.KeyWindow.RootViewController;

            while (vc.PresentedViewController != null)
                vc = vc.PresentedViewController;

            return vc;
        }
    }
}
elmernocon commented 7 years ago

@jfversluis any news?

elmernocon commented 7 years ago

this will fix this. it's not showing because importMenu.View.Bounds is set initially to (x: 0, y: 0, width: yourscreen's width, height: yourscreen's height); anchoring the presPopover to that point, instead of bottom of the screen.

if (presPopover != null)
{
    CGRect rect = importMenu.View.Bounds;
    rect.Y = rect.Height;
    presPopover.SourceView = importMenu.View;
    presPopover.SourceRect = rect;
    ...
}