0

I develop Xamarin Forms app. And I use QLPreviewController for ios. I want to close copy text/text share, if document has text. Is it possible and how? I searched it as native. Some sources redirect me to UIDocumentInteractionControllerDelegate. But I didn't understand how can i do it.

Thanks in advance.

enter image description here

Edit: My UIDocumentInteractionController implementation:

var firstController = UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers.First().ChildViewControllers.Last().ChildViewControllers.First();
                    var navcontroller = firstController as UINavigationController;

                    var uidic = UIDocumentInteractionController.FromUrl(new NSUrl(file, true));
                    uidic.Delegate = new DocInteractionController(navcontroller);
                    uidic.PresentPreview(true);

public class DocInteractionController : UIDocumentInteractionControllerDelegate
    {
        UINavigationController navigationController;
        
        public DocInteractionController(UINavigationController _navigationController)
        {
            navigationController = _navigationController;
        }
    }
Pelin Konaray
  • 272
  • 1
  • 3
  • 15
  • Do you want to hide the whole menu or just remove the copy and share options ? You could post the relevant code that you had done so that I can check it on my side . – Lucas Zhang Nov 16 '20 at 08:12
  • Actually i couldn't do anything yet. I only saw [this thread](https://stackoverflow.com/questions/6918210/how-to-disable-qlpreviewcontroller-print-button) There it is said: _As for disabling cut/copy/paste, you can't do that with QLPreviewController. You might be able to do that with UIDocumentInteractionController_ So that I only implement UIDocumentInteractionController. And I searched an event for disabling cut/copy/paste. I added implementation of UIDocumentInteractionController to my question. – Pelin Konaray Nov 16 '20 at 08:38
  • I want to know is it possible. Because my sending thread only said that you can do it with UIDocumentInteractionController. But how? It doesn't say how. So I couldn't be sure it is possible. – Pelin Konaray Nov 16 '20 at 08:43
  • So you want to hide the whole menu ? – Lucas Zhang Nov 16 '20 at 08:44
  • Yes, whichever is possible – Pelin Konaray Nov 16 '20 at 08:46
  • In your case it would be better to use WebView to preview files like pdf . – Lucas Zhang Nov 16 '20 at 09:23

1 Answers1

1

In your case it would be better to use WebView to preview files like pdf .

In forms

Create a custom webview

public class MyWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
            returnType: typeof(string),
            declaringType: typeof(MyWebView),
            defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }
}

in Android project


using Android.Content;
using Android.Net.Http;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using App32;
using App32.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(MyWebView), typeof(CustomWebViewRenderer))]
namespace App32.Droid
{
    public class CustomWebViewRenderer : WebViewRenderer
    {
        Context _context;
        public CustomWebViewRenderer(Context context) : base(context)
        {
            _context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                Android.Webkit.WebView web_view = new Android.Webkit.WebView(_context);
                web_view.LoadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url="+((MyWebView)Element).Uri);
           
                SetNativeControl(web_view);
                Control.Settings.JavaScriptEnabled = true;
               
            }
        }
    }

    
}

in iOS project

using System;
using App32;
using App32.iOS;
using Foundation;
using ObjCRuntime;
using UIKit;
using WebKit;
using Xamarin.Forms.Platform.iOS;

[assembly: Xamarin.Forms.ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace App32.iOS
{
    public class MyWebViewRenderer : ViewRenderer<MyWebView, WKWebView>
    {
        WKWebView _wkWebView;

        protected override void OnElementChanged(ElementChangedEventArgs<MyWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                var config = new WKWebViewConfiguration();
                _wkWebView = new WKWebView(Frame, config);
               
                SetNativeControl(_wkWebView);
            }

            if(e.NewElement!=null)
            {
                var webview = Element as MyWebView;

                var url = webview.Uri;

                Control.LoadRequest(new NSUrlRequest(new NSUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + webview.Uri)));
            }

        }
        

    }

  
}

in xaml

Now you can use it in xaml like (you could open a new ContentPage that contains the WebView)

<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

        <local:MyWebView Uri="https://www.pdfpdf.com/samples/Sample1.PDF"/>

</StackLayout>
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22