问题描述:

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?

网友答案:

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.

网友答案:

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! :)

相关阅读:
Top