Open Shared Folder In Xamarin Forms iOS

Introduction

In this article, we will learn how to open Shared Folder in iOS in the Xamarin Forms application using NSUrl. This is very important if you download a file and you want to open Shared Folder in iOS Xamarin mobile application.

We will be using NSUrl to achieve this requirement.

Steps

Let's create Xamarin mobile application in Visual Studio.

Assign a name for the application.

Specify the location to store the application.

Now the project is created for iOS and Android. It contains three projects Shared, iOS, and Android in one solution. Please set the iOS project as a start-up project.

Also, choose your Simulator to run this application. In my case, I have selected iPhone 12 to run the application.

Now create one helper class in the Shared iOS project.

Add the below code into this file:

using System;
using OpenFileManager.Services;
using Xamarin.Forms;

namespace OpenFileManager.Services
{
    public class OpenFileManagerHelper
    {
        public void OpenSharedDocument()
        {
            DependencyService.Get<IOpenFileManagerHelper>().OpenFileManager();
        }
    }
}

Create an interface in the Shared project.

Add the below code into this file,

using System;
namespace OpenFileManager.Services
{
    public interface IOpenFileManagerHelper
    {
        void OpenFileManager();
    }
}

Now create a class in the iOS project.

Add the below code into this file,

using System;
using OpenFileManager.Services;
using OpenFileManager.iOS;
using UIKit;
using Xamarin.Forms;
using Foundation;

[assembly: Dependency(typeof(OpenFileManagerInterface))]
namespace OpenFileManager.iOS
{
    public class OpenFileManagerInterface : IOpenFileManagerHelper
    {
        public void OpenFileManager()
        {
            var url = new NSUrl($"shareddocuments:");
            UIApplication.SharedApplication.OpenUrl(url);
        }
    }
}

Now create a class in the Android project.

Add the below code into this file,

using System;
using Android.Content;
using OpenFileManager.Services;
using OpenFileManager.Droid;
using Xamarin.Forms;
using Application = Android.App.Application;

[assembly: Dependency(typeof(OpenFileManagerInterface))]
namespace OpenFileManager.Droid
{
    public class OpenFileManagerInterface : IOpenFileManagerHelper
    {
        public void OpenSharedDocument()
        {
            var intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
            intent.AddFlags(ActivityFlags.NewTask);
            string package_name = "my.android.package.name";
            var uri = Android.Net.Uri.FromParts("package", package_name, null);
            intent.SetData(uri);
            Application.Context.StartActivity(intent);
        }
    }
}

Now open ManinPage.xml and add one button to open this Shared Folder.

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="OpenFileManager.MainPage">

    <StackLayout Padding="50">
        <Button x:Name="OpenFileManager" Clicked="OpenFileManager_Clicked" Text="Open"></Button>
    </StackLayout>

</ContentPage>

MainPage.xaml.cs

Add below code to open Shared Folder in iOS,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenFileManager.Services;
using Xamarin.Forms;

namespace OpenFileManager
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        void OpenFileManager_Clicked(System.Object sender, System.EventArgs e)
        {
            OpenFileManagerHelper h = new OpenFileManagerHelper();
            h.OpenSharedDocument();
        }
    }
}

Build your solution and now run an iOS project to see the output.

Output

Main Window

After clicking on the Open button then Shared Folder will open in iOS,

This will open your Shared folder in iOS. 

Thank You.


Similar Articles