Xamarin.iOS UIDocumentPickerViewController

3
我有一个关于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不会启动,除了设置我的provisioning、entitlements和info.plist之外,我是否需要其他选项/权限才能使用iOS的文档选择器?
1个回答

3

最近我在我的Xamarin Forms应用程序中尝试了它,现在它运行得很好:

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
    }

如有问题,请回复。


你好,我的主要问题是documentPicker.DidPickDocumentAtUrls事件无法启动,你之前有做过任何事情吗,比如授权或者证书方面的设置。谢谢。 - jbtamares
尝试更新我的代码并查看它是否有效! - FreakyAli
没问题,如果你遇到任何进一步的问题,请告诉我。 - FreakyAli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接