通过openURL获取我的应用程序正在打开的文件的URL。[英] Getting the URL of a file my app is opening via openURL:

本文是小编为大家收集整理的关于通过openURL获取我的应用程序正在打开的文件的URL。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我使用自定义文件类型配置了我的应用程序.我添加了info.plist文件(文档类型和导出的UTI)中所需的信息.现在,iOS识别我的文件类型,并将其与应用程序关联.因此,例如,当我打开邮件时,我将文件视为附件时,我可以单击它,然后选择使用我的应用程序打开.

如何从应用程序中获取文件的URL?我尝试在UIApplication上使用OpenUrl,但它不起作用.

有人知道该怎么做吗?

推荐答案

找到的解决方案

我说我需要Xamarin/C#的解决方案,但是我要求提供标准的Objective-C代码以稍后翻译C#,因为我找不到任何内容.

最后,这是对我有用的解决方案:

在 appdelegate.cs 文件中,我添加此方法:

public override bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
     NSNotificationCenter.DefaultCenter.PostNotification (NSNotification.FromName ("OpenMyFile", url));
     return true;
}

然后在我的主控制器中,我将其添加到" ViewDidload"方法中:

NSNotificationCenter.DefaultCenter.AddObserver ("OpenMyFile", openFileVoid);

,在同一类中,我添加了一种新方法:

public async void openFileVoid (NSNotification notification)
{
    NSUrl _filePath = (NSUrl)notification.Object;
    // Do what you need with this file path
}

我希望对某人有用! :)

其他推荐答案

我认为这与-[UIApplication openURL:]无关.

正如苹果doc :

您收到有关文件的信息 在application:willFinishLaunchingWithOptions:或 application:didFinishLaunchingWithOptions:您应用程序的方法 代表.如果您的应用程序处理自定义文件类型,则必须 实施此委托方法(而不是 applicationDidFinishLaunching:方法)并使用它来初始化您的 应用程序.

选项字典传递给 application:willFinishLaunchingWithOptions:或 application:didFinishLaunchingWithOptions:方法包含信息 关于要打开的文件.

您可以在文档中阅读更多信息.

本文地址:https://www.itbaoku.cn/post/2354211.html

问题描述

I configured my app with a custom file type. I added the information required in the Info.plist file (Document Types and Exported UTIs). Now iOS recognizes my file type and associates it with the app. So, for example, when I open Mail, and I see my file as an attachment, I can click on it and select to open with my app.

How I can get the URL of the file from my app? I tried to use OpenUrl on UIApplication, but it does not work.

Anyone know how to do that?

推荐答案

SOLUTION FOUND

I state that I need a solution for Xamarin/C#, but I asked for a standard objective-c code to later translate in C#, becouse I could not find anything about that.

At the end this is the solution that worked for me:

In the AppDelegate.cs file I add this method:

public override bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
     NSNotificationCenter.DefaultCenter.PostNotification (NSNotification.FromName ("OpenMyFile", url));
     return true;
}

Then in my main controller, I add this in the "ViewDidLoad" method:

NSNotificationCenter.DefaultCenter.AddObserver ("OpenMyFile", openFileVoid);

And in the same class I add a new method:

public async void openFileVoid (NSNotification notification)
{
    NSUrl _filePath = (NSUrl)notification.Object;
    // Do what you need with this file path
}

I hope can be useful to someone! :)

其他推荐答案

I think it has nothing to do with -[UIApplication openURL:].

As it was stated in Apple Doc :

You receive information about the file to be opened in the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method of your application delegate. If your application handles custom file types, you must implement this delegate method (instead of the applicationDidFinishLaunching: method) and use it to initialize your application.

The options dictionary passed to the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method contains information about the file to be opened.

You can read about it more in the doc.