Forum Cross Platform with Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Launcher.OpenAsync vs Share.RequestAsync

shuffshuff Member ✭✭
edited December 2019 in Cross Platform with Xamarin

I'm trying to understand the difference between the Launcher.OpenAsync and Share.RequestAsync.

According to this documentation: https://docs.microsoft.com/en-us/xamarin/essentials/launcher?tabs=ios

One would think that this code:

                    Launcher.OpenAsync(new OpenFileRequest
                    {
                        File = new ReadOnlyFile(file)
                    });

Would open file which is a Word document with the file Extension DOCX in Word which is installed on the iPhone. However instead it gives this prompt:

When I use this code:

                Share.RequestAsync(new ShareFileRequest
                {
                    Title = Title,
                    File = new ShareFile(file, mimeType)
                });

I get a VERY similar prompt (yet slightly different) to open the document:

However, both seem to have the issue of the bug which I logged here almost a month ago: https://github.com/xamarin/Essentials/issues/982

What I would like to do is just open the file in the Word app which is installed on the phone but neither of these work as expected. I'm sure there is supposed to be some type of functionality different between Launcher and Share but I'm not seeing it.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Launcher.OpenAsync() is try to open the file in another application so that this file or uri could be previewed there. And Share.RequestAsync() will transfer this file to the destination application then run the next steps(i.e. raise a new post on Facebook).
    All the application installed on the iPhone will be listed if they support to open this kind of files. And the word application won't have the higher priority so maybe it will locate under the More button. You could adjust the order there.

  • shuffshuff Member ✭✭

    So if you have a Word document and you want to send it specifically to Word (assuming they have Word installed), how would you do that?

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you only want to open the word application you could try to use url scheme to open a certain app. I found the url scheme for word is ms-word:ofe|u|<URL> following this documentation: https://docs.microsoft.com/en-us/office/client-developer/integration/integrate-with-office-from-ios-applications?redirectedfrom=MSDN
    Then I create a dependency service to open word like:

    [assembly: Dependency(typeof(ShareImplementation))]
    namespace App.iOS
    {
        public class ShareImplementation : IShareManager
        {
            public void ShareToWorkd()
            {
                var bundlePath = NSBundle.MainBundle.PathForResource("sample.docx", null);
    
                var folderPath = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User).FirstOrDefault();
                var filePath = Path.Combine(folderPath, "sample.docx");
    
                if (File.Exists(filePath)) File.Delete(filePath);
                File.Copy(bundlePath, filePath);
    
                var sharePath = new NSString("ms-word:ofe|u|" + filePath);            
                var customUrl = new NSUrl(sharePath.CreateStringByAddingPercentEscapes(NSStringEncoding.UTF8));
                UIApplication.SharedApplication.OpenUrl(customUrl, new NSDictionary(), (success) =>
                {
                });
            }
        }
    }
    

    The original file located in the project's catalog. I copied it to my sandbox folder and pass it to the word application. The word application could be opened correctly, however, the file couldn't be read as expected. I've no idea which part stuck this process. Maybe, the url is not completed as required so I think using Share plugin is a better choice for now.

Sign In or Register to comment.