【问题标题】:Xamarin forms: Word doc is not opening in ios appXamarin 表单:Word 文档未在 ios 应用程序中打开
【发布时间】:2020-07-12 19:58:44
【问题描述】:

我正在使用Device.OpenUri(new Uri(word_doc_url)); 在我的应用程序中打开 word/pdf 文档。 Pdf 文档可以正常打开,但 word 文档没有在 ios 设备中打开。在 iPhone 上打开 word doc 时显示 AccessdeniedAccess 消息。

截图

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

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

截图

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

还尝试了根据 [this thread][3] 在应用中打开 word doc 的 webview 功能。

PCL 中的 PdfWebView.cs

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 中。

【问题讨论】:

  • 您可以使用QLPreviewController在您的应用程序中打开本地word文件。
  • @LucasZhang-MSFT Word 文件未保存在本地。
  • @LucasZhang-MSFT no

标签: pdf xamarin.forms webview ms-word


【解决方案1】:

您可以使用库 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,而不是添加 web 视图(使用页面渲染器)。

选项 2

您可以添加 Microsoft Office Preview 前缀

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

它在我这边工作得很好。您的 url 似乎会直接下载文件,因此请确保该 url 可访问。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 2021-12-15
相关资源
最近更新 更多