我以前用过this way在默认应用程序中打开文档,但从 iOS 10 开始,它在 iPhone 上不起作用(应用程序崩溃),但在 iPad 上可以正常工作。
从 DependencyService
打开文件的正确方法是什么?
由于我目前没有可测试的 iOS 10 设备,因此我无法找到该错误。
最佳答案
您需要了解您尝试打开的应用程序的 URL Scheme; URL Schemes 是应用程序之间通信的唯一方式。
您没有指定要打开的应用程序,因此我在下面提供了示例代码,展示了如何使用 URL Schemes 打开“设置”应用程序、“邮件”应用程序和 App Store 应用程序(针对特定应用程序)。
Apple 有 additional documentation on URL Schemes here
using UIKit;
using MessageUI;
using Foundation;
using Xamarin.Forms;
using SampleApp.iOS;
[assembly: Dependency(typeof(DeepLinks_iOS))]
namespace SampleApp.iOS
{
public class DeepLinks_iOS : IDeepLinks
{
public void OpenStoreLink()
{
Device.BeginInvokeOnMainThread(() => UIApplication.SharedApplication.OpenUrl(new NSUrl("https://appsto.re/us/uYHSab.i")));
}
public void OpenFeedbackEmail()
{
MFMailComposeViewController mailController;
if (MFMailComposeViewController.CanSendMail)
{
mailController = new MFMailComposeViewController();
mailController.SetToRecipients(new string[] { "support@gmail.com" });
mailController.SetSubject("Email Subject String");
mailController.SetMessageBody("This text goes in the email body", false);
mailController.Finished += (object s, MFComposeResultEventArgs args) =>
{
args.Controller.DismissViewController(true, null);
};
var currentViewController = GetVisibleViewController();
currentViewController.PresentViewController(mailController, true, null);
}
}
public void OpenSettings()
{
Device.BeginInvokeOnMainThread(() => UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString)));
}
static UIViewController GetVisibleViewController()
{
var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
if (rootController.PresentedViewController == null)
return rootController;
if (rootController.PresentedViewController is UINavigationController)
{
return ((UINavigationController)rootController.PresentedViewController).TopViewController;
}
if (rootController.PresentedViewController is UITabBarController)
{
return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
}
return rootController.PresentedViewController;
}
}
}
关于c# - 在默认应用程序中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39572646/