Xamarin 表单:Word 文档未在 ios 应用程序中打开

Xamarin forms: Word doc is not opening in ios app

我正在使用 Device.OpenUri(new Uri(word_doc_url)); 在我的应用程序中打开 word/pdf 文档。 Pdf 文档可以正常打开,但 word 文档无法在 ios 设备中打开。在 iPhone.

上打开 Word 文档时显示 AccessdeniedAccess 消息

截图

[![在此处输入图片描述][1]][1]

我在 info.plist 上添加了 NSAppTransportSecurity 键。

截图

[![在此处输入图片描述][2]][2]

还根据[此主题][3]尝试了用于在应用程序中打开 word 文档的 webview 功能。

PdfWebView.cs 在 PCL

public class PdfWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
    returnType: typeof(string),
    declaringType: typeof(PdfWebView),
    defaultValue: default(string));
    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }
}

PdfWebViewRenderer.cs

[assembly: ExportRenderer(typeof(PdfWebView), typeof(PdfWebViewRenderer))]
namespace Projectname.iOS.Renderer
{
    class PdfWebViewRenderer : ViewRenderer<PdfWebView, UIWebView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<PdfWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                SetNativeControl(new UIWebView());
            }
            if (e.OldElement != null)
            {
                // Cleanup
            }
            if (e.NewElement != null)
            {
                var customWebView = Element as PdfWebView;
                string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", WebUtility.UrlEncode(customWebView.Uri)));
                Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false)));
                Control.ScalesPageToFit = true;
            }
        }
    }
}

在XAML和XAML.cs中:

<local:PdfWebView 
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"
    x:Name="pdf_Webview"/>

pdf_Webview.Source = pdfurl;
pdf_Webview.Uri = pdfurl;   

但是 pdf 和 word 文件没有在 webview 中显示。

您可以使用库 QuickLook 内联或从本地预览文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

using Foundation;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using QuickLook;
//...[assembly]
namespace xxx.iOS
{

    public class PreviewItem : QLPreviewItem
    {
       
        NSUrl fileUrl;

        public override string ItemTitle
        {
            get { return fileUrl.LastPathComponent; }
        }

        public override NSUrl ItemUrl
        {
            get { return fileUrl; }
        }

        public PreviewItem(string filePath)
        {
            fileUrl = NSUrl.FromString(filePath);

          
        }
    }

    class MyWebViewRenderer:ViewRenderer<PdfWebView, UIWebView>,IQLPreviewControllerDataSource,IQLPreviewControllerDelegate
    {
        string filePath;

        public IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
        {
            return new PreviewItem(filePath);
        }

        public nint PreviewItemCount(QLPreviewController controller)
        {
            return 1;
        }

        [Export("previewController:shouldOpenURL:forPreviewItem:")]
        public bool ShouldOpenUrl(QLPreviewController controller,NSUrl url, QLPreviewItem item)
        {
            return true;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<PdfWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                SetNativeControl(new UIWebView());
            }
            if (e.OldElement != null)
            {
                // Cleanup
            }
            if (e.NewElement != null)
            {
                var customWebView = Element as PdfWebView;
                filePath = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", WebUtility.UrlEncode(customWebView.Uri)));

                var previewController = new QLPreviewController();
               

                previewController.WeakDelegate = this;
                previewController.DataSource = this;

                previewController.CurrentPreviewItemIndex = 0;

                var currentViewController = topViewControllerWithRootViewController(UIApplication.SharedApplication.Delegate.GetWindow().RootViewController);
                currentViewController.PresentViewController(previewController, true, null);
            }
        }




        UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
        {
            if (rootViewController is UITabBarController)
            {
                UITabBarController tabBarController = (UITabBarController)rootViewController;
                return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
            }
            else if (rootViewController is UINavigationController)
            {
                UINavigationController navigationController = (UINavigationController)rootViewController;
                return topViewControllerWithRootViewController(navigationController.VisibleViewController);
            }
            else if (rootViewController.PresentedViewController != null)
            {
                UIViewController presentedViewController = rootViewController.PresentedViewController;
                return topViewControllerWithRootViewController(presentedViewController);
            }
            else
            {
                return rootViewController;
            }
        }

    }
}

您最好直接在 iOS 中的 ContentPage 上打开使用 QLPreviewController 而不是添加 webview(使用页面渲染器)。

选项 2

您可以添加 Microsoft Office Preview 前缀

Device.OpenUri(new Uri("https://view.officeapps.live.com/op/view.aspx?src=your 
 file url"));

我这边很好用。您的 url 似乎会直接下载文件,因此请确保 url 可以访问。