Pdf Xamarin表单:Word文档未在ios应用程序中打开
我正在使用Pdf Xamarin表单:Word文档未在ios应用程序中打开,pdf,xamarin.forms,webview,ms-word,Pdf,Xamarin.forms,Webview,Ms Word,我正在使用Device.OpenUri(新的Uri(word\u doc\u url))用于在我的应用程序中打开word/pdf文档。Pdf文档可以正常打开,但word文档无法在ios设备中打开。在iPhone上打开word文档时显示AccessdeniedAccess消息 屏幕截图 [![在此处输入图像描述][1][1] 我已在我的info.plist上添加了NSAppTransportSecurity键 屏幕截图 [![在此处输入图像描述][2][2] 还根据[此线程][3]尝试了在应用程序
Device.OpenUri(新的Uri(word\u doc\u url))代码>用于在我的应用程序中打开word/pdf文档。Pdf文档可以正常打开,但word文档无法在ios设备中打开。在iPhone上打开word文档时显示AccessdeniedAccess
消息
屏幕截图
[![在此处输入图像描述][1][1]
我已在我的info.plist上添加了NSAppTransportSecurity
键
屏幕截图
[![在此处输入图像描述][2][2]
还根据[此线程][3]尝试了在应用程序中打开word文档的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;
}
}
}
}
[程序集:ExportRenderer(typeof(PdfWebView)、typeof(PdfWebViewRenderer))]
命名空间Projectname.iOS.Renderer
{
类PdfWebViewRenderer:ViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control==null)
{
SetNativeControl(新UIWebView());
}
if(e.OldElement!=null)
{
//清理
}
if(例如NewElement!=null)
{
var customWebView=作为PdfWebView的元素;
string fileName=Path.Combine(NSBundle.MainBundle.BundlePath,string.Format(“Content/{0}”,WebUtility.UrlEncode(customWebView.Uri));
LoadRequest(新的NSUrlRequest(新的NSUrl(文件名,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_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;
}
}
}
}
对我来说效果很好。您的url似乎将直接下载文件,因此,请确保url是可访问的。您可以使用QLVeviewController
在应用程序中打开本地word文件。@LucasZhang MSFT word文件未保存在本地。@LucasZhang MSFT您能提供一个示例项目吗?您可以提供您的项目,以便我可以改进它。好的,我将首先提供一个示例。别忘了删除你的个人信息。创建了一个示例项目,请看一看。
Device.OpenUri(new Uri("https://view.officeapps.live.com/op/view.aspx?src=your
file url"));