Xamarin.iOS UIDocumentPickerViewController

时间:2018-08-28 06:08:52

标签: xamarin.forms xamarin.ios

我对iOS文档选择器有疑问。

我已包含iCloud容器/已启用iCloud进行配置。还添加了Xamarin.iOS本身所需的Cloudkit要求。但是,启动弹出菜单以显示iOS的浏览文件菜单时遇到问题。

var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Import);

documentPicker.DidPickDocument += DocumentPicker_DidPickDocument;
documentPicker.WasCancelled += DocumentPicker_WasCancelled;
documentPicker.DidPickDocumentAtUrls += DocumentPicker_DidPickDocumentAtUrls;
documentPicker.WasCancelled += DocumentPicker_WasCancelled;

private void DocumentPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
{
     var control = (UIDocumentPickerViewController)sender;
     foreach (var url in e.Urls)
         DocumentPicker_DidPickDocument(control, new UIDocumentPickedEventArgs(url));

         control.Dispose();
}

似乎事件DidPickDocument / DidPickDocumentAtUrls事件不会启动,除了设置我的配置,权利,info.plist之外,是否还有其他需要我使用的选项/权限,以便我使用iOS的文档选择器?

1 个答案:

答案 0 :(得分:1)

最近我做了,它在我的xamarin表单应用程序中可以正常工作:

private void ShowDocsPicker()
    {
        try
        {
            var docPicker = new UIDocumentPickerViewController(new string[]
            { UTType.Data, UTType.Content }, UIDocumentPickerMode.Import);
            docPicker.WasCancelled += DocPicker_WasCancelled;
            docPicker.DidPickDocumentAtUrls += DocPicker_DidPickDocumentAtUrls;
            var _currentViewController = GetCurrentUIController();
            if (_currentViewController != null)
                _currentViewController.PresentViewController(docPicker, true, null);
        }
        catch (Exception ex)
        {
          //Exception Logging
        }
    }

获取当前的UIViewController像这样:

  public UIViewController GetCurrentUIController()
    {
        UIViewController viewController;
        var window = UIApplication.SharedApplication.KeyWindow;
        if (window == null)
        {
            return null;
        }

        if (window.RootViewController.PresentedViewController == null)
        {
            window = UIApplication.SharedApplication.Windows
                     .First(i => i.RootViewController != null &&
                                 i.RootViewController.GetType().FullName
                                 .Contains(typeof(Xamarin.Forms.Platform.iOS.Platform).FullName));
        }

        viewController = window.RootViewController;

        while (viewController.PresentedViewController != null)
        {
            viewController = viewController.PresentedViewController;
        }

        return viewController;
    }

然后添加像这样的文档所选事件:

 private void DocPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e)
    {
     //Action to perform on document pick
    }

在查询的情况下还原。