0

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
BadStorm
  • 126
  • 8
  • Thanks for sharing the solution, but please put it in an answer below rather than into the body of your question. [It's perfectly acceptable to answer your own question](http://meta.stackexchange.com/questions/120215/ended-up-solving-my-own-problem-question-what-to-do-with-the-post?lq=1). – jscs May 31 '14 at 18:32

2 Answers2

2

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

BadStorm
  • 126
  • 8
1

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.

3329
  • 1,411
  • 13
  • 17