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

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

iOS: View is not in the window hierarchy! #21

Closed elmernocon closed 7 years ago

elmernocon commented 7 years ago

Xam.Plugin.FilePicker Version 1.1.0 Xamarin Forms Version 2.3.3.180 Xamarin.iOS Version 10.3

private async void PickFile () { try { FileData fileData = await CrossFilePicker.Current.PickFile(); } catch (Exception ex) {} }

2017-02-07 11:29:54.298 ProjectiOS[1363:1213422] Warning: Attempt to present <UIDocumentMenuViewController: 0x1599a6a10> on <Xamarin_Forms_Platform_iOS_PlatformRenderer: 0x157eaa060> whose view is not in the window hierarchy!

elmernocon commented 7 years ago
public class FilePickerImplementation : NSObject, IUIDocumentMenuDelegate, IFilePicker
{

...

        public void DidPickDocumentPicker (UIDocumentMenuViewController documentMenu, UIDocumentPickerViewController documentPicker)
        {
            ...

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

...

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

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

            ...
        }

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

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

            return vc;
        }
}
riandesign commented 7 years ago

@elmernocon , how did you fix it? I didn't get the second comment. Where I find that code?

elmernocon commented 7 years ago

@riandesign , Hi, any chance you're still using the NuGet package? because that one is heavily outdated. If you are, I suggest cloning this repo and rebuilding the .DLL(s). There you'll find the FilePickerImplementation.cs for iOS. This issue seems to happen when you are presenting a view controller not from the most active view controller, you can check by calling FileData fileData = await CrossFilePicker.Current.PickFile(); from your very first page.

riandesign commented 7 years ago

@elmernocon alright! Can you tell me how compile the repro GitHub and get the DLL(s)? PS: I installed it from Visual Studio, and the version is 1.2.1. Is it outdated?

riandesign commented 7 years ago

@elmernocon also, in your code, what is GetActiveViewController()? Can you share the code, please?

elmernocon commented 7 years ago

@riandesign , here you go recompiling .DLL(s). GetActiveViewController() is a helper method that tries to get the most visible view controller by reiterating from the very first view controller (your main page) to the very last (your child/grandchild/grandgrandchild/grandgrandgrand... page).

riandesign commented 7 years ago

@elmernocon that's the same as if I build the solution and go to \bin\iPhone\Release, right? There'll be the DLL's file, aren't they? Or am I wrong?

elmernocon commented 7 years ago

@riandesign , yup.

riandesign commented 7 years ago

@elmernocon I build the DLL's from this repro GitHub, but now when I build on iOS I got this: System.Exception: Error getting value from 'DataArray' on 'Plugin.FilePicker.Abstractions.FileData'.