【问题标题】:Open file in default app在默认应用中打开文件
【发布时间】:2016-09-19 11:59:11
【问题描述】:

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

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

由于我目前没有要测试的 iOS 10 设备,因此我无法解决该错误。

【问题讨论】:

  • 这最终可能会对您有所帮助:riccardo-moschetti.org/2014/10/03/…
  • 那你要知道URL scheme
  • 能不能在模拟器上试一试,看看哪里出错了? “它不起作用”是什么意思? (应用程序崩溃,没有任何反应,Quick Look 出现空白屏幕,还是别的什么?)。我没有看到对 Quick Look 框架行为的任何记录更改。
  • @dylansturg 我试过了,但正如您在链接中看到的那样,它没有使用快速查看。但是感谢您的帮助,我使用 Quick Look 修复了它。

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


【解决方案1】:

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

您没有指定您要打开哪个应用程序,因此我在下面提供了示例代码,展示了如何使用 URL 方案打开设置应用程序、邮件应用程序和 App Store 应用程序(针对特定的应用程序)。

苹果有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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多