在默认应用中打开文件

时间:2016-09-19 11:59:12

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

我之前使用this way在默认应用中打开文档,但从iOS 10开始它无法在iPhone上运行(应用程序崩溃),但在iPad上运行正常。

DependencyService打开文件的正确方法是什么?

由于我目前没有要测试的iOS 10设备,我无法得到错误。

1 个答案:

答案 0 :(得分:0)

您需要知道您尝试打开的应用的网址方案; URL方案是在应用程序之间进行通信的唯一方式。

您没有指定要尝试打开的应用,因此我在下面提供了示例代码,演示了如何使用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;
        }
    }
}