c# - 在默认应用程序中打开文件

标签 c# ios xamarin xamarin.ios xamarin.forms

我以前用过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/

相关文章:

ios - Swift 空数组 for 循环

ios - 旋转时自动调整 UITableView 标题的大小(iPad 上的 MoSTLy)

C# 文件 - 输出类型 : console

ios - UIView 和 subview 委托(delegate)

c# - Unity3D 中的 Encoding.UTF32.GetBytes 返回额外的零

c# - 生成随机的唯一值 C#

c# - 我需要屏蔽导体吗(Caliburn Micro)

c# - 从 IIS 运行应用程序 (asp.net) 时无法创建 excel 文件

iphone - 将文件上传到 Google 云端硬盘时出错

authentication - 带有 Google API 的 Xamarin.Auth : Renew credentials?