0

I have a xamarin forms app that can download files to "on my iphone" folder. But when I download a file on phone, I can go there and I can share it with another app.

enter image description here

But I want to prevent this. When I download a file from my app, I want the file not to be uploaded to another device from "on my iphone".

How to prevent this? Probably I can prevent this with mdm, but how? Is there a way to prevent it with mdm managed app configuration. Some of my customers said that we can prevent this with the plist file in mdm. But I have very little information about mdm. How to do it with mdm? I need a solution for ios and android. But especially for ios.

Thank you in advance.

Pelin Konaray
  • 272
  • 1
  • 3
  • 15
  • You can remove the button by yourself. Create a dependency service and use a custom QLPreviewController to open the file. And hide the button in that QLPreviewController like [this thread](https://stackoverflow.com/questions/59442408/hide-or-disable-share-button-from-uidocumentinteractioncontroller-in-swift-5). – nevermore Nov 09 '20 at 06:13

1 Answers1

0

Thank you for reply Jack.

I looked your sending thread and I tried it. But it didn't work for me. I created MyUINavigationItem that is inherited from UINavigationItem. And I override SetRightBarButtonItem null. After I override UINavigationItem on PdfPreviewController that is inherited from QLPreviewController. And I set NavigationItem as MyUINavigationItem object. But it doesn't work for me. My codes is like these:

public class DocumentView : IDocumentView
{
    void IDocumentView.DocumentView(string file, string title)
    {
         PdfPreviewController previewController = new PdfPreviewController();
         if (File.Exists(file))
         {
              previewController.NavigationItem.SetRightBarButtonItem(null, false); //I tried this line as both comment and not comment but it didn't work
              previewController.NavigationItem.SetRightBarButtonItems(null, false); //I tried this line as both comment and not comment but it didn't work
              previewController.DataSource = new PDFPreviewControllerDataSource(NSUrl.FromFilename(file), title);
              UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null);

          }
    }
}
public class PdfPreviewController : QLPreviewController
{
     MyUINavigationItem item = new MyUINavigationItem();
     public override UINavigationItem NavigationItem => item;
}
public class MyUINavigationItem : UINavigationItem
{
    public MyUINavigationItem()
    {

    }

    public override void SetRightBarButtonItem(UIBarButtonItem item, bool animated)
    {
        //base.SetRightBarButtonItem(item, animated); //I tried this line as comment but it didn't work or
        //base.SetRightBarButtonItem(null, animated); //I tried this line but it didn't work
    }

    public override void SetRightBarButtonItems(UIBarButtonItem[] items, bool animated)
    {
        //base.SetRightBarButtonItems(items, animated); //I tried this line as comment but it didn't work or
        //base.SetRightBarButtonItems(null, animated); //I tried this line but it throwed exception
        //base.SetRightBarButtonItems(new UIBarButtonItem[0], animated); //I tried this line but it didn't work
    }
}

But after your suggestion I looked for enable or hide share button. And I could access child view controller of UINavigationController and I set RightBarButtonItem enable value false.

public class PdfPreviewController : QLPreviewController
{
        public override void ViewDidAppear(bool animated)
        {
            try
            {
                ActionMenuControl();
            }
            catch (Exception ex)
            {
            }
        }

        public override void ViewDidLayoutSubviews()
        {
            try
            {
                ActionMenuControl();
            }
            catch (Exception ex)
            {
            }
        }

        public void ActionMenuControl()
        {
            try
            {
                if (this.ChildViewControllers.Length != 0)
                {
                    var navigationController = this.ChildViewControllers.First() as UINavigationController;
                    if (navigationController.View.Subviews.Length != 0)
                    {
                        var layoutContainerView = navigationController.View.Subviews.FirstOrDefault(x => x is UINavigationBar) as UINavigationBar;
                        if (layoutContainerView != null) 
                        { 
                            if (layoutContainerView.Items.Length != 0)
                            {
                                var item = layoutContainerView.Items[0];

                                if (item.RightBarButtonItems.Length != 0)
                                    item.RightBarButtonItem.Enabled = false;
                            }
                        }
                    }

                    var toolbar = navigationController.Toolbar;
                    if (toolbar != null) 
                    { 
                        if(toolbar.Items.Length != 0)
                        {
                            toolbar.Items[0].Enabled = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                
            }
        }
}

It works for right bar. But if opens mp3 files, share button appear on toolbar (on bottom) And I didn't access it. How can I do it for mp3 files? How to access toolbar? (Actually it doesn't work when opens and not move it. But if I open it and move it, it works (because it enters ViewDidLayoutSubviews events))

Sorry for long reply. But I wanted to tell about what I did. Because maybe I did missed something.

Pelin Konaray
  • 272
  • 1
  • 3
  • 15